I call HTTP request with retrofit library and get results from an API. I configure log in retrofit library so i could see what's going on. The response is a simple primitive integer type. The problem is that it always returns zero. Response class(LoginModel) is also a classic class with one setter and one getter for integer type.
public class LoginModel {
@SerializedName("result")
private int result;
public void setResult(int result){
this.result = result;
}
public int getResult(){
return result;
}
}
Below image is my log which you can see i got response 10 but when i log getResult in onResponse function, it prints always zero.
Log.i("BODY : ", String.valueOf(response.body().getResult()));
I can't figure out whats wrong. I appreciate any help. thanks
UPDATE: full code of calling API
Call<LoginModel> isSended = apiService.sendCode(phoneNumber);
isSended.enqueue(new Callback<LoginModel>() {
@Override
public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {
Log.i("BODY : ", String.valueOf(response.body().getResult()));
if (!response.isSuccessful()) {
Toast.makeText(context, context.getResources().getString(R.string.errorServer), Toast.LENGTH_LONG).show();
dismissProgressBarDialog();
return;
}
myTimer = new MyTimer(SECOND_TIME, 1000);
views.reactionActivationPress(response.body().getResult());
}
@Override
public void onFailure(Call<LoginModel> call, Throwable t) {
Log.i(Constant.NETWORK_LOG, "sendCode : " + t.toString());
dismissProgressBarDialog();
Toast.makeText(MyApplication.getContext(), "Server Problem", Toast.LENGTH_LONG).show();
}
});
Code of Gson and Retrofit creation:
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.addInterceptor(chain -> {
Request request = chain.request();
Request newRequest;
newRequest = request.newBuilder()
.addHeader(HEADER_VERSION_CODE, String.valueOf(BuildConfig.VERSION_CODE))
.build();
return chain.proceed(newRequest);
})
.build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
ApiClient.getClient().create(ApiService.class).sendCode(phoneNumber).enqueue(new Callback<LoginModel>() {
@Override
public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {
Log.i("BODY : ", String.valueOf(response.body().getResult()));
if (!response.isSuccessful()) {
Toast.makeText(context, context.getResources().getString(R.string.errorServer), Toast.LENGTH_LONG).show();
dismissProgressBarDialog();
return;
}
myTimer = new MyTimer(SECOND_TIME, 1000);
views.reactionActivationPress(response.body().getResult());
}
@Override
public void onFailure(Call<LoginModel> call, Throwable t) {
Log.i(Constant.NETWORK_LOG, "sendCode : " + t.toString());
dismissProgressBarDialog();
Toast.makeText(MyApplication.getContext(), "Server Problem", Toast.LENGTH_LONG).show();
}
});
There is no problem with the code. It works well. Make sure you are using Gson with retrofit. I've tested the code and its working well. No need to make changes in model class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With