Search code examples
retrofitsquare

Retrofit Response Parsing


im looking for the possibility to control when the Response will be parsed as json. I call an Endpoint where i actually cant modify the result, i get an ugly 'ok' from that dumb PHP Script. Problem is that the JSON Parser tries to parse this and fails:

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)

My RX Subscription gets calledthat the Parsing when wrong:

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

I cant null for example the JSON Parser/Converter, null results in an NullPointerException getting thrown by Retrofit.


Solution

  • Use Response as the return type (or Callback generic parameter) which won't trigger parsing the body using the specified Converter. This object gives you a representation of the HTTP response where you can query the status code, headers, and body directly, if you need to.