美文网首页
Retrofit报错onFailure: java.lang.I

Retrofit报错onFailure: java.lang.I

作者: 晚路歌 | 来源:发表于2020-05-27 16:34 被阅读0次

    Retrofit报错log:

    TAG: onFailure: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
    

    报错原因是json解析错误,使用的是

    addConverterFactory(GsonConverterFactory.create())
    

    解决方法:
    用阿里的开源框架Fastjson自己写一个Factory
    添加Fastjson依赖

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

    FastJsonConverterFactory.java

    import java.lang.annotation.Annotation;
    import java.lang.reflect.Type;
    
    import okhttp3.RequestBody;
    import okhttp3.ResponseBody;
    import retrofit2.Converter;
    import retrofit2.Retrofit;
    
    public class FastJsonConverterFactory extends Converter.Factory{
    
        public static FastJsonConverterFactory create() {
            return new FastJsonConverterFactory();
        }
    
        /**
         * 需要重写父类中responseBodyConverter,该方法用来转换服务器返回数据
         */
        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            return new FastJsonResponseBodyConverter<>(type);
        }
    
        /**
         * 需要重写父类中responseBodyConverter,该方法用来转换发送给服务器的数据
         */
        @Override
        public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
            return new FastJsonRequestBodyConverter<>();
        }
    }
    

    FastJsonResponseBodyConverter.java

    import com.alibaba.fastjson.JSON;
    import java.io.IOException;
    import java.lang.reflect.Type;
    import okhttp3.ResponseBody;
    import okio.BufferedSource;
    import okio.Okio;
    import retrofit2.Converter;
    
    class FastJsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
        
    private final Type type;
    
        public FastJsonResponseBodyConverter(Type type) {
            this.type = type;
        }
    
        /*
         * 转换方法
         */
        @Override
        public T convert(ResponseBody value) throws IOException {
            BufferedSource bufferedSource = Okio.buffer(value.source());
            String tempStr = bufferedSource.readUtf8();
            bufferedSource.close();
            return JSON.parseObject(tempStr, type);
        }
    }
    

    FastJsonRequestBodyConverter.java

    import com.alibaba.fastjson.JSON;
    import java.io.IOException;
    import okhttp3.MediaType;
    import okhttp3.RequestBody;
    import retrofit2.Converter;
    
    public class FastJsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
        private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
    
        @Override
        public RequestBody convert(T value) throws IOException {
            return RequestBody.create(MEDIA_TYPE, JSON.toJSONBytes(value));
        }
    }
    

    使用方法:

    addConverterFactory(FastJsonConverterFactory.create())
    

    相关文章

      网友评论

          本文标题:Retrofit报错onFailure: java.lang.I

          本文链接:https://www.haomeiwen.com/subject/ozawahtx.html