In retrofit to map json response to pojo usually we do this
@POST
Call<User> getDataFromServer(@Url String url, @Body HashMap<String,Object> hashMap);
ApiCalls api = retrofit.create(ApiCalls.class);
Call<User> call = api.getDataFromServer(StringConstants.URL,hashMap);
call.enqueue(new Callback<User>() {
//Response and failure callbacks
}
where User is my Pojo class. But for every other request i need to make different pojo and write same code for retrofit call.I want to make a single method for calling api and pass the respective pojo class to retrofit call. like this
ApiCalls api = retrofit.create(ApiCalls.class);
Call<*ClassPassed*> call = api.getDataFromServer(StringConstants.URL,hashMap);
call.enqueue(new Callback<*ClassPassed*>() {
//Response and failure callbacks
}
so now i can any pojo class to single method and get the response.This is just to avoid rewriting the same code again and again.is this possible
Update To elaborate more:
Suppose I need to make two requests. First one is to get userDetails and the other is patientDetails.So i have to create two model classes User and Patient. So in retrofit api i'll be having something like this
@POST
Call<User> getDataFromServer(@Url String url, @Body HashMap<String,Object> hashMap);
@POST
Call<Patient> getDataFromServer(@Url String url, @Body HashMap<String,Object> hashMap);
and in my FragmentUser and FragmentPatient class i'll be doing this
ApiCalls api = retrofit.create(ApiCalls.class);
Call<User> call = api.getDataFromServer(StringConstants.URL,hashMap);
call.enqueue(new Callback<User>() {
//Response and failure callbacks
}
ApiCalls api = retrofit.create(ApiCalls.class);
Call<Patient> call = api.getDataFromServer(StringConstants.URL,hashMap);
call.enqueue(new Callback<Patient>() {
//Response and failure callbacks
}
but here the code is repaeting just beacuse of different pojo classes.I need to repeat the same code in every other fragments for different requests. So i need to make a generic method where it can accept any pojo class and then from fragment i'll be just passing the pojo to be mapped.
Do it like this :
Use standard generics, with a little bit of hacking around
Define your interface like this
and call for creating api client use a helper method
But despite of the fact how many times it has been said here, I am gonna say it again... Don't do this .. You are giving up the whole type safety and contract validation that Retrofit is offering .. That is actually the most exciting thing about it..
In Order to generalize what you want, you can simply serialize your POJO, and then you can just pass your POJO to the method as is. when you serialize with Objects it basically converts it to string, which are later converted to one big Json String, which are easier to transfer and manipulate.
A quick example would be:
example POJO implementing the serialization, here you should make sure the strings in the
Map<String,Object>
correspond to what the server is expecting to get, and this method should be different in each POJO:The serialization interface (so you can implement it across other POJOs)
And a Json parser you shoul probably have anyways:
And last, your API defintion:
And method, which should sit in a general class that manages your requests:
I return the response through a listener, that's one example of what you can do depending on your response.
Hope this helps!
First Create Interface:
Now create class:
Now in any activity just use:
I use following approach: First I have implemented custom Call
It wrappes
Call<Tin>
and converts it's result to<Tout>
by converter.For your service you must create service interface, that return JsonObject for single object and JsonArray for arrays
Then wrap it with generic class, with converters from JsonElement to any Type
<T>
:GenericListType is ParaterizedType. It is used for passing type parameter to gson for
List<T>
Then you can instantiate
ApiCallsGeneric
with type you want.You can build main pojo like this
And call like this