大佬教程收集整理的这篇文章主要介绍了Retrofit 2.0 + GSON无法为接口调用无参数构造函数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
接口(interface GetPhones
)和模型类(class GetPhones
)的名称相同。
我认为您正在此行中使用接口:
Call<ArrayList<GetPhones>> getPhones();
但这应该是您的模型类。检查导入部分是否有此名称,或重命名模型类以确保您没有将其混合。
我在应用中使用Retrofit 2.0。一切都很好,但是当我开始不带参数的请求时,GSON返回:
Unable to invoke no-args constructor for interface
com.example.project.API.GetPhones. Register an InstanceCreator
with Gson for this type may fix this problem.
这是我的API接口:
public interface GetPhones {
@GET("phones.php")
Call<ArrayList<GetPhones>> getPhones();
}
和模型类:
public class GetPhones {
int id;
char[] iso;
String name;
String phone_1;
String phone_2;
String phone_3;
}
和代码片段:
Retrofit retrofit = new Retrofit.builder()
.baseUrl(URL_API)
.client(SSLSuppressClient.trustcert())
.addConverterFactory(GsonConverterFactory.create())
.build();
GetPhones getPhonesInfo = retrofit.create(GetPhones.class);
Call<GetPhones> call = getPhonesInfo.getPhones();
call.enqueue(new CallBACk<GetPhones>() {
@Override
public void onResponse(Response<GetPhones> response,Retrofit retrofit) {
Toast.makeText(getActivity(),"success!",Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(getActivity(),"Failure!",Toast.LENGTH_SHORT).show();
Log.d("LOG",t.getmessage());
}
});
以上是大佬教程为你收集整理的Retrofit 2.0 + GSON无法为接口调用无参数构造函数全部内容,希望文章能够帮你解决Retrofit 2.0 + GSON无法为接口调用无参数构造函数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。