【问题标题】:Retrofit Response Parsing改造响应解析
【发布时间】:2014-10-07 15:19:06
【问题描述】:

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

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

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

【问题讨论】:

    标签: retrofit square


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 2021-12-09
      • 2021-01-22
      • 2019-03-01
      • 2018-09-04
      • 1970-01-01
      相关资源
      最近更新 更多