Skip to content

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

Closed
@andishejavan

Description

@andishejavan

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

ly1054 commented on Sep 6, 2016

@ly1054

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

JakeWharton

JakeWharton commented on Sep 6, 2016

@JakeWharton
Collaborator

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

developer--

developer-- commented on Nov 18, 2016

@developer--

would be great

lordplagus02

lordplagus02 commented on Jul 20, 2017

@lordplagus02

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

JakeWharton commented on Jul 20, 2017

@JakeWharton
Collaborator
lordplagus02

lordplagus02 commented on Jul 20, 2017

@lordplagus02

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

shahsurajk commented on Aug 10, 2017

@shahsurajk

@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

LessTalk commented on Dec 13, 2017

@LessTalk

oh This will be very troublesome

shahsurajk

shahsurajk commented on Dec 13, 2017

@shahsurajk

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

khushbu-shah

khushbu-shah commented on Oct 5, 2019

@khushbu-shah

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

emanuelthedude commented on Jul 27, 2021

@emanuelthedude

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

morder commented on Aug 29, 2022

@morder

It needs to fix proguard rules

antonio195

antonio195 commented on May 2, 2023

@antonio195

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @JakeWharton@morder@developer--@emanuelthedude@lordplagus02

        Issue actions

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