我使用JacksonConverter和Retrofit-2.0.0-beta2来解决json响应的反序列化问题。当我通过请求时,我得到了例外:
10-19 16:14:21.918 31481-31481 /? W / System.err的: com.fasterxml.jackson.core.JsonParseException:意外的字符 (''(代码65279 / 0xfeff)):期望一个有效值(数字,字符串, array,object,'true','false'或'null')10-19 16:14:21.918 31481-31481 /? W / System.err:at [来源: java.io.InputStreamReader@edac0ef; line:2,column:2] 10-19 16:14:21.918 31481-31481 /? W / System.err:at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419) 10-19 16:14:21.918 31481-31481 /? W / System.err:at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508) 10-19 16:14:21.919 31481-31481 /? W / System.err:at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:437) 10-19 16:14:21.919 31481-31481 /? W / System.err:at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1462) 10-19 16:14:21.919 31481-31481 /? W / System.err:at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:683) 10-19 16:14:21.919 31481-31481 /? W / System.err:at com.fasterxml.jackson.databind.ObjectReader._initForReading(ObjectReader.java:1346) 10-19 16:14:21.919 31481-31481 /? W / System.err:at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1252) 10-19 16:14:21.919 31481-31481 /? W / System.err:at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:880) 10-19 16:14:21.919 31481-31481 /? W / System.err:at retrofit.JacksonResponseBodyConverter.convert(JacksonResponseBodyConverter.java:33) 10-19 16:14:21.919 31481-31481 /? W / System.err:at retrofit.JacksonResponseBodyConverter.convert(JacksonResponseBodyConverter.java:23) 10-19 16:14:21.919 31481-31481 /? W / System.err:at retrofit.OkHttpCall.parseResponse(OkHttpCall.java:148)10-19 16:14:21.919 31481-31481 /? W / System.err:at retrofit.OkHttpCall.access $ 100(OkHttpCall.java:29)10-19 16:14:21.919 31481-31481 /? W / System.err:at retrofit.OkHttpCall $ 1.onResponse(OkHttpCall.java:94)10-19 16:14:21.919 31481-31481 /? W / System.err:at com.squareup.okhttp.Call $ AsyncCall.execute(Call.java:168)10-19 16:14:21.919 31481-31481 /? W / System.err:at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33) 10-19 16:14:21.919 31481-31481 /? W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 10-19 16:14:21.919 31481-31481 /? W / System.err:at java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 10-19 16:14:21.919 31481-31481 /? W / System.err:at java.lang.Thread.run(Thread.java:818)
我无法更改响应JSON。看看我的Retrofit配置:
sInstance = new RetrofitRequestCreator();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
sInstance.mRetrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(
JacksonConverterFactory.create(mapper)).build();
Gradle.build
...
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-jackson:2.0.0-beta2'
...
有没有办法逃脱这个角色?感谢您的所有回复
答案 0 :(得分:-2)
您需要包含所有依赖项。
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.fasterxml.jackson:jackson-datatype-json-org:1.8.0'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-jackson:2.0.0-beta2'