I'am using okhttp to operate on api requests but i've no idea how to create instance of pojo class from JSON response.
CodePudding user response:
You can use gson/Jackson library to deserialise response. Consider the service you are calling returns below response
{
"data": {"name": "Foo", "id": 1}
}
Then your POJO/models would look like
public class ServiceResponse {
private Data data;
}
public class Data {
private String name;
private int id;
}
And your simple OkHttp client call is like
Response response = client.newCall(request).execute();
Then you can deserialise response using Jackson like this
ServiceResponse = new ObjectMapper().readValue(response.body().string(), ServiceResponse.class)
Refer this link for detailed explanation