Retrofit请求API返回整数始终为0的问题排查求助
Let’s break down why you’re seeing 0 instead of the expected 10 in your response—here are the most likely issues and how to fix them:
1. Mismatch between API Response Format and Your LoginModel
You mentioned the API returns a "simple basic integer type," but your LoginModel is designed to parse a JSON object with a result field (e.g., {"result":10}).
If the API is actually sending a raw integer (like just 10 as plain text or a raw JSON number), Gson can’t map that to your LoginModel—since there’s no result key to match, it leaves the result variable at its default value of 0.
Fix Options:
- Option 1: Use
Call<Integer>for raw integer responses
Update your Retrofit service method to return a rawIntegerinstead ofLoginModel:
Then adjust your callback to handle the integer directly:// In your ApiService interface @POST("your-sendcode-endpoint") Call<Integer> sendCode(@Query("phone") String phoneNumber);Call<Integer> isSended = apiService.sendCode(phoneNumber); isSended.enqueue(new Callback<Integer>() { @Override public void onResponse(Call<Integer> call, Response<Integer> response) { if (!response.isSuccessful()) { Toast.makeText(context, context.getResources().getString(R.string.errorServer), Toast.LENGTH_LONG).show(); dismissProgressBarDialog(); return; } if (response.body() != null) { int result = response.body(); Log.i("BODY : ", String.valueOf(result)); myTimer = new MyTimer(SECOND_TIME, 1000); views.reactionActivationPress(result); } dismissProgressBarDialog(); } @Override public void onFailure(Call<Integer> call, Throwable t) { Log.i(Constant.NETWORK_LOG, "sendCode : " + t.toString()); dismissProgressBarDialog(); Toast.makeText(MyApplication.getContext(), "Server Problem", Toast.LENGTH_LONG).show(); } }); - Option 2: Fix
LoginModelif API returns a JSON object
Double-check your logs for the raw response body—if it’s actually{"result":10}, confirm that:- The
@SerializedName("result")matches the exact key (JSON keys are case-sensitive!) - You’ve added a no-arg constructor to
LoginModel(Gson sometimes requires this for deserialization):public class LoginModel { @SerializedName("result") private int result; // Add empty constructor public LoginModel() {} public void setResult(int result){ this.result = result; } public int getResult(){ return result; } }
- The
2. Wrong Order of Retrofit Converter Factories
You added ScalarsConverterFactory before GsonConverterFactory in your Retrofit setup. Retrofit uses converters in the order they’re registered—if your API response has a Content-Type that ScalarsConverterFactory can handle (like text/plain), it will try to parse the response first, which can cause silent failures when you’re expecting a JSON object.
If you need both converters, move GsonConverterFactory first (since most of your API calls likely use JSON):
retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(gson)) // Gson first .addConverterFactory(ScalarsConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build();
3. Verify Logging Interceptor Configuration
Make sure your logging interceptor is showing the full raw response body—sometimes logs might truncate or show only metadata. Confirm that the response you’re seeing (10) is exactly what the API is sending, not a processed version.
内容的提问来源于stack exchange,提问作者Mehran Zamani

