0

我正在寻找控制何时将响应解析为 json 的可能性。我调用了一个实际上无法修改结果的端点,我从那个愚蠢的 PHP 脚本中得到了一个丑陋的“ok”。问题是 JSON Parser 尝试解析它并失败:

D/Retrofit( 6334): <--- HTTP 200 https://somewhere.com/endpoint.php?idfv=android_id&UserInterfaceIdiom=hammerhead&systemVersion=1.0&status=Not+Set&batteryLevel=100%25&localizedModel=Nexus+5&systemName=Android+OS&bundleShortVersion=1&language=eng&batteryState=USB&bundeIdentifier=com.packagename&bundleVersion=1.0 (475ms)
D/Retrofit( 6334): : HTTP/1.1 200 OK
D/Retrofit( 6334): Connection: Keep-Alive
D/Retrofit( 6334): Content-Type: text/html
D/Retrofit( 6334): Date: Tue, 07 Oct 2014 14:49:20 GMT
D/Retrofit( 6334): Keep-Alive: timeout=5, max=100
D/Retrofit( 6334): OkHttp-Received-Millis: 1412693360928
D/Retrofit( 6334): OkHttp-Response-Source: NETWORK 200
D/Retrofit( 6334): OkHttp-Selected-Protocol: http/1.1
D/Retrofit( 6334): OkHttp-Sent-Millis: 1412693360859
D/Retrofit( 6334): Server: Apache
D/Retrofit( 6334): Vary: Accept-Encoding
D/Retrofit( 6334): X-Powered-By: PHP/5.4.4-14+deb7u14
D/Retrofit( 6334): OK
D/Retrofit( 6334): <--- END HTTP (2-byte body)

我的 RX 订阅在错误时被称为解析:

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1

例如 JSON Parser/Converter,我不能为null,null 会导致 Retrofit 抛出 NullPointerException。

4

1 回答 1

2

Response用作返回类型(或Callback泛型参数),它不会触发使用指定的Converter. 此对象为您提供 HTTP 响应的表示,如果需要,您可以在其中直接查询状态代码、标头和正文。

于 2014-10-07T15:59:48.563 回答