Closed
Description
how define generic T class in interface api and rest mansger ?
api this
@GET("api/customer/")
Call<DataModelResponse<T>> getList(@Query("take") int page, @Query("skip") int step);
call rest manager this
mManager = new ServiceGenerator();
Call<DataModelResponse<T>> listCall = mManager.getService().getList(ClientConfigs.PAGAE_RECYCLER, skip);
rest manager this
public Api getService() {
return Api;
}
Activity
ly1054 commentedon Sep 6, 2016
"Method return type must not include a type variable or wildcard " it seems to be clear
JakeWharton commentedon Sep 6, 2016
You cannot. Type information needs to be fully known at runtime in order for deserialization to work.
developer-- commentedon Nov 18, 2016
would be great
lordplagus02 commentedon Jul 20, 2017
So for example, a request such as:
Call<T[]> call = apiService.getResources(...);
whereT extends Resource
would not be possible? (Resource
being a class from the moshi-jsonapi library, which allows you to extend a class as aResource
.)Would I then need to create a different retrofit API interface for each of these
Resources
?Sorry to bring up old issues...
JakeWharton commentedon Jul 20, 2017
lordplagus02 commentedon Jul 20, 2017
So... If I have 6 different types of resources, I'm going to create 6 different interfaces, and this is how people have been doing it the whole time? I'm sorry I'm just not sure what the alternative is here when trying to keep my code DRY.
shahsurajk commentedon Aug 10, 2017
@lordplagus02
How about this, define the Call so as to return a
JsonElement
as the class type, something like this,Call<JsonElement> getObjectByID();
and then parse the jsonElement yourselfCustomObject1 customObject1 = new Gson().fromJson(response.body().getAsJsonObject().toString(), CustomObject1.class);
CustomObject2 customObject2 = new Gson().fromJson(response.body().getAsJsonObject().toString(), CustomObject2.class);
So here, the parsing dependency is upon you and not retrofit. Hope it helps.
this way you can create just one interface for all object by ID queries, alternatively you can also add query map to add additional options and set null if not required.
LessTalk commentedon Dec 13, 2017
oh This will be very troublesome
shahsurajk commentedon Dec 13, 2017
You can create these methods once and then you're done. A utility sort of thing
khushbu-shah commentedon Oct 5, 2019
yes It will work but May I have some more in explanation if possible with example with 2 or more service call
Thank you shahsurajk
emanuelthedude commentedon Jul 27, 2021
I get this error because of proguard. In my interface I had a function that was not used anymore. Also the class of the return type was not used anymore in the project. I think proguard removed the return type class and made the return type of the function Call or Call<?>. The result was the same IllegalArgumentException as soon as the first http call started.
morder commentedon Aug 29, 2022
It needs to fix proguard rules
antonio195 commentedon May 2, 2023
It has worked for me using Kotlin. But is this the best way to do this? Doesn't this generic form bring problems with memory leaks?