Retrofit 2.0 + GSON无法为接口调用无参数构造函数 [英] Retrofit 2.0 + GSON Unable to invoke no-args constructor for interface
问题描述
我在我的应用中使用Retrofit 2.0.一切都很好,但是当我开始不带参数的请求时,GSON返回:
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 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());
}
});
我试图为GetPhones.class构造无参数的构造函数,但它没有任何改变.
I've tried to make no-args constructor for GetPhones.class, but it doesn't change anything.
推荐答案
接口(interface GetPhones
)和模型类(class GetPhones
)的名称相同.
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.
这篇关于Retrofit 2.0 + GSON无法为接口调用无参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!