Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalArgumentException: Method return type must not include a type variable or wildcard: retrofit2.Call<model.DataModelResponse<T>> #2012

Closed
andishejavan opened this issue Sep 6, 2016 · 13 comments

Comments

@andishejavan
Copy link

andishejavan commented Sep 6, 2016

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;
    }
@ly1054
Copy link

ly1054 commented Sep 6, 2016

"Method return type must not include a type variable or wildcard " it seems to be clear

@JakeWharton
Copy link
Member

You cannot. Type information needs to be fully known at runtime in order for deserialization to work.

@developer--
Copy link

would be great

@lordplagus02
Copy link

So for example, a request such as:
Call<T[]> call = apiService.getResources(...); where T extends Resource would not be possible? (Resource being a class from the moshi-jsonapi library, which allows you to extend a class as a Resource.)
Would I then need to create a different retrofit API interface for each of these Resources?

Sorry to bring up old issues...

@JakeWharton
Copy link
Member

JakeWharton commented Jul 20, 2017 via email

@lordplagus02
Copy link

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
Copy link

@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 yourself

CustomObject1 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
Copy link

oh This will be very troublesome

@shahsurajk
Copy link

You can create these methods once and then you're done. A utility sort of thing

@khushbu-shah
Copy link

khushbu-shah commented 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
Copy link

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
Copy link

morder commented Aug 29, 2022

It needs to fix proguard rules

@antonio195
Copy link

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?

retrofit2
retrofit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests