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.
Here's my API interface:
public interface GetPhones {
@GET("phones.php")
Call<ArrayList<GetPhones>> getPhones();
}
and model class:
public class GetPhones {
int id;
char[] iso;
String name;
String phone_1;
String phone_2;
String phone_3;
}
and code in fragment:
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());
}
});
I've tried to make no-args constructor for GetPhones.class, but it doesn't change anything.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…