解决:Retrofit请求返回: Expected a string but was BEGIN_OBJECT at line 1 column 4832 path $

Posted 森然献凉i

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决:Retrofit请求返回: Expected a string but was BEGIN_OBJECT at line 1 column 4832 path $相关的知识,希望对你有一定的参考价值。

最近在尝试使用玩android开放平台的api做一个玩Android app,在我请求首页文章列表时卡住了两天,当我使用ResponseBody作为请求体的时候,正常返回数据,可当我使用javabean对象作为请求体的时候,直接就给我返回了 onFailure,就让我很疑惑。困扰我许久

 java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 4832 path $.data.datas[7].tags[0]

其实这个报错并不是来自Retrofit,而是来自Gson,这个错误的意思就是我想帮你解析成String而你返回的数据是一个对象。

在使用Retrofit的时候,大部分同学使用的都是Gson解析,不过Retrofit官方并没有提供FastJson的解析库。不过阿里的大佬已经写好了

https://github.com/alibaba/fastjson/blob/master/src/main/java/com/alibaba/fastjson/support/retrofit/Retrofit2ConverterFactory.java

或者你只需要新建一个

Retrofit2ConverterFactory.java

package com.cdw.study7;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

public class Retrofit2ConverterFactory extends Converter.Factory {
    private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
    private static final Feature[] EMPTY_SERIALIZER_FEATURES = new Feature[0];

    private ParserConfig parserConfig = ParserConfig.getGlobalInstance();
    private int featureValues = JSON.DEFAULT_PARSER_FEATURE;
    private Feature[] features;

    private SerializeConfig serializeConfig;
    private SerializerFeature[] serializerFeatures;

    public Retrofit2ConverterFactory() {
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, //
                                                            Annotation[] annotations, //
                                                            Retrofit retrofit) {
        return new ResponseBodyConverter<ResponseBody>(type);
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, //
                                                          Annotation[] parameterAnnotations, //
                                                          Annotation[] methodAnnotations, //
                                                          Retrofit retrofit) {
        return new RequestBodyConverter<RequestBody>();
    }

    public ParserConfig getParserConfig() {
        return parserConfig;
    }

    public Retrofit2ConverterFactory setParserConfig(ParserConfig config) {
        this.parserConfig = config;
        return this;
    }

    public int getParserFeatureValues() {
        return featureValues;
    }

    public Retrofit2ConverterFactory setParserFeatureValues(int featureValues) {
        this.featureValues = featureValues;
        return this;
    }

    public Feature[] getParserFeatures() {
        return features;
    }

    public Retrofit2ConverterFactory setParserFeatures(Feature[] features) {
        this.features = features;
        return this;
    }

    public SerializeConfig getSerializeConfig() {
        return serializeConfig;
    }

    public Retrofit2ConverterFactory setSerializeConfig(SerializeConfig serializeConfig) {
        this.serializeConfig = serializeConfig;
        return this;
    }

    public SerializerFeature[] getSerializerFeatures() {
        return serializerFeatures;
    }

    public Retrofit2ConverterFactory setSerializerFeatures(SerializerFeature[] features) {
        this.serializerFeatures = features;
        return this;
    }

    final class ResponseBodyConverter<T> implements Converter<ResponseBody, T> {
        private Type type;

        ResponseBodyConverter(Type type) {
            this.type = type;
        }

        public T convert(ResponseBody value) throws IOException {
            try {
                return JSON.parseObject(value.string()
                        , type
                        , parserConfig
                        , featureValues
                        , features != null
                                ? features
                                : EMPTY_SERIALIZER_FEATURES
                );
            } finally {
                value.close();
            }
        }
    }

    final class RequestBodyConverter<T> implements Converter<T, RequestBody> {
        RequestBodyConverter() {
        }

        public RequestBody convert(T value) throws IOException {
            byte[] content = JSON.toJSONBytes(value
                    , serializeConfig == null
                            ? SerializeConfig.globalInstance
                            : serializeConfig
                    , serializerFeatures == null
                            ? SerializerFeature.EMPTY
                            : serializerFeatures
            );

            return RequestBody.create(MEDIA_TYPE, content);
        }
    }
}

导入依赖

implementation 'com.alibaba:fastjson:1.1.70.android'

 

在初始化Retrofit的地方,将Gson更换为ConvertFactory:

                .addConverterFactory(new Retrofit2ConverterFactory())

结束

以上是关于解决:Retrofit请求返回: Expected a string but was BEGIN_OBJECT at line 1 column 4832 path $的主要内容,如果未能解决你的问题,请参考以下文章

解决:Retrofit请求返回: Expected a string but was BEGIN_OBJECT at line 1 column 4832 path $

解决:Retrofit请求返回: Expected a string but was BEGIN_OBJECT at line 1 column 4832 path $

retrofit: Expected Android API level 21+ but was 19

Retrofit请求数据对错误以及网络异常的处理

Retrofit请求数据对错误以及网络异常的处理

Retrofit源码分析&实践Retrofit 多BaseUrl问题解决