0

使用Retrofit配合GsonConverter,我们能很好地将网络响应内容转换成对应的对象。比如像下面这样。

Retrofit网络接口方法

1
2
3
4
5
6
7

public interface DroidNetwork {
@GET(“/content/test.json”)
Call jsonData();

@GET(“/content/helloworld.txt”)
Call plainText();
}

对应的应用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

val retrofit = Retrofit.Builder()
.baseUrl(“https://asset.droidyue.com/”)
.addConverterFactory(GsonConverterFactory.create())
.build()

val droidNetwork = retrofit.create(DroidNetwork::class.java)

droidNetwork.jsonData().enqueue(object : Callback {
override fun onFailure(call: Call?, t: Throwable?) {
t?.printStackTrace()
}

override fun onResponse(call: Call?, response: Response?) {
dumpMessage(“onResponse content=${response?.body()}”)
}
})

上面的方法执行都很正常,可是执行这个方法的时候就会报错。

1
2
3
4
5
6
7
8
9
10

droidNetwork.plainText().enqueue(object : Callback {
override fun onFailure(call: Call?, t: Throwable?) {
Exception(“causedByPlainText”, t)?.printStackTrace()
}

override fun onResponse(call: Call?, response: Response?) {
dumpMessage(“onResponse content=${response}”)
}

})

崩溃的信息如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

W System.err: java.lang.Exception: causedByPlainText
W System.err: at com.example.secoo.retrofitconvertor.MainActivity$doNetworkRequest$2.onFailure(MainActivity.kt:41)
W System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$2.run(ExecutorCallAdapterFactory.java:80)
W System.err: at android.os.Handler.handleCallback(Handler.java:891)
W System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W System.err: at android.os.Looper.loop(Looper.java:207)
W System.err: at android.app.ActivityThread.main(ActivityThread.java:7470)
W System.err: at java.lang.reflect.Method.invoke(Native Method)
W System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
W System.err: Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
W System.err: at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1568)
W System.err: at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1409)
W System.err: at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:593)
W System.err: at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
W System.err: at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:393)
W System.err: at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:390)
W System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
W System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
W System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:223)
W System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
W System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
W System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
W System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W System.err: at java.lang.Thread.run(Thread.java:784)

admin 已回答的问题 20 9 月, 2023