关于Gson解析失败:Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

找了半天,终于搞定了,不得不说,这是个坚强的bug,真的是个坚强的bug...

一把辛酸了,,啥也不说了,希望遇到一样坑的小伙伴 别放弃,你们还有我。。。。哈哈

解决办法:划重点。原来用的 GsonConverterFactory 解决一些接口返回值规范的一点问题都没有,偏偏有些接口真的不听话,成功返回个字符串"OK",失败,返回一大串Json异常信息,乖乖嘞。。。 下面这个方法这样写就可以啦

关于Gson解析失败:Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $_第1张图片

奉上LenientGsonConverterFactory.java类

/**
 * Created by Jane on 2018/8/16.
 */
public class LenientGsonConverterFactory extends Converter.Factory {

    /**
     * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
     * decoding from JSON (when no charset is specified by a header) will use UTF-8.
     */
    public static LenientGsonConverterFactory create() {
        return create(new Gson());
    }

    /**
     * Create an instance using {@code gson} for conversion. Encoding to JSON and
     * decoding from JSON (when no charset is specified by a header) will use UTF-8.
     */
    public static LenientGsonConverterFactory create(Gson gson) {
        return new LenientGsonConverterFactory(gson);
    }

    private final Gson gson;

    private LenientGsonConverterFactory(Gson gson) {
        if (gson == null) throw new NullPointerException("gson == null");
        this.gson = gson;
    }

    @Override
    public Converter responseBodyConverter(Type type, Annotation[] annotations,
                                                            Retrofit retrofit) {
        TypeAdapter adapter = gson.getAdapter(TypeToken.get(type));
        return new LenientGsonResponseBodyConverter<>(gson, adapter);
    }

    @Override
    public Converter requestBodyConverter(Type type,
                                                          Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        TypeAdapter adapter = gson.getAdapter(TypeToken.get(type));
        return new LenientGsonRequestBodyConverter<>(gson, adapter);
    }
    public class LenientGsonResponseBodyConverter implements Converter {
        private final Gson gson;
        private final TypeAdapter adapter;

        LenientGsonResponseBodyConverter(Gson gson, TypeAdapter adapter) {
            this.gson = gson;
            this.adapter = adapter;
        }

        @Override
        public T convert(ResponseBody value) throws IOException {
            JsonReader jsonReader = gson.newJsonReader(value.charStream());
            jsonReader.setLenient(true);
            try {
                return adapter.read(jsonReader);
            } finally {
                value.close();
            }
        }
    }
    public class LenientGsonRequestBodyConverter implements Converter {
        private  final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
        private  final Charset UTF_8 = Charset.forName("UTF-8");

        private final Gson gson;
        private final TypeAdapter adapter;

        LenientGsonRequestBodyConverter(Gson gson, TypeAdapter adapter) {
            this.gson = gson;
            this.adapter = adapter;
        }

        @Override
        public RequestBody convert(T value) throws IOException {
            Buffer buffer = new Buffer();
            Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
            JsonWriter jsonWriter = gson.newJsonWriter(writer);
            jsonWriter.setLenient(true);
            adapter.write(jsonWriter, value);
            jsonWriter.close();
            return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
        }
    }
}

 其实真相就是。。在stackoverflow 上找到的,在这里,谢过各位大神,在我菜鸟成长路上,又给我加了些能量值 哈哈

原地址:https://stackoverflow.com/questions/27485346/malformedjsonexception-with-retrofit-api

希望遇到同类问题的小伙伴,一起交流 吼吼!!

 

你可能感兴趣的:(错误集锦,开发工具,知识条目总结,Use,Gson解析不规则Json数据)