Retrofit 2.0 + GSON Unable to invoke no-args constructor for interface

前端 未结 3 863
借酒劲吻你
借酒劲吻你 2020-12-19 02:20

I\'m using Retrofit 2.0 in my app. Everything was quite good, but when I started request with no args, GSON returns:

Unable to invoke no-args constructor for         


        
相关标签:
3条回答
  • 2020-12-19 02:55

    You have the same name for interface (interface GetPhones) and model class (class GetPhones).

    I think you are using interface in this line:

    Call<ArrayList<GetPhones>> getPhones();
    

    But it should be your model class. Check import section for it or rename model class to be sure that you are not mixing it.

    0 讨论(0)
  • 2020-12-19 02:58

    This was really helpful. I had the same issue and it worked by replacing Call<MyObject> with MyObject in the interface where we write the function for the API call request followed by removing .enque at the call site.

    0 讨论(0)
  • 2020-12-19 03:10

    If anyone in 2019 comes across this and is using Kotlin, coroutines and at least Retrofit 2.6.0, returning a Call<MyObject> instance while the api method is suspended, produces the same error message, which is a little confusing.

    The solution is to replace Call<MyObject> with MyObject in the interface definition, and remove ?.execute()?.body() (or equivalent) at the call site.

    EDIT:

    The reason I think most people will stumble across this is that they want to wrap their suspended Retrofit responses in something to handle errors in a seamless way.

    There is an issue on this located here, perhaps Retrofit will deal with this in the future. I ended up using the kind solution provided here.

    0 讨论(0)
提交回复
热议问题