I’m using google-http-java-client-1.10.3-beta for making RESTfull Json requests to a service provided by an external source.
I need to parse the Json that’s coming in the response to build some POJOs to my app.
For that, I’m using HttpResonse method ‘parse as’:
response.parseAs(SomeModel.class)
But I’m getting this exception:
java.lang.IllegalArgumentException: No parser defined for Content-Type: application/json
at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:497)
The responded json has this format:
[{“key” : {“key” : “value”, “key” : “value”}}]
I had experminted some issues with other Json libraries when handling responses with square brackets, could that be the problem here?
Any ideas?
Responding to myself, the api needs you to set manually a JsonObjectParser object to the request involved.
A JsonObjectParser might be a GsonParser or JacksonParser or any other implented parser.
So, in order to set a GsonParser to your request you could use this code:
Then you will be able to do:
And all your POJOs will be automatically generated.
Note: I’ve upgraded my google-http-java-client version to 1.11.0-beta, an earlier version won’t return a HttpRequest object when calling setParser so be carefull when using the presented code, you may need a void instead of a function.