問題

我使用改進庫呼叫HTTP請求並從API獲得結果.我在改進庫中配置日誌,以便我可以看到發生了什麼.響應是一個簡單的原始整數型別. 問題是它總是返回零.響應類(LoginModel)也是一個具有一個setter和一個整數型別的getter的經典類.

 public class LoginModel {

  @SerializedName("result")
  private int result;

  public void setResult(int result){
    this.result = result;
  }

  public int getResult(){
    return result;
  }
}
 

下面的影象是我的日誌,你可以看到我得到了響應10,但是當我在onResponse函式中記錄getResult時,它總是列印零.

 Log.i("BODY : ", String.valueOf(response.body().getResult()));
 

enter image description here

我無法弄清楚什麼是錯誤的.我感謝任何幫助.謝謝

更新:呼叫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();
                }
            });
 

  最佳答案

程式碼中的問題是您正在嘗試在respons中訪問兩次結果請嘗試如下

只宣告一次response.body().getResult()

     Call<LoginModel> isSended = apiService.sendCode(phoneNumber);
                    isSended.enqueue(new Callback<LoginModel>() {
                        @Override
                        public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {

                        dismissProgressBarDialog();
                        LoginModel model = response.body().getResult();                   
                        Log.i("BODY : ", String.valueOf(model));
                            if (!response.isSuccessful()) {
                                Toast.makeText(context, context.getResources().getString(R.string.errorServer), Toast.LENGTH_LONG).show();

                                return;
                            }
                            myTimer = new MyTimer(SECOND_TIME, 1000);
                            views.reactionActivationPress(model);
                        }

                        @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();
                        }
                    });
 

  相同標籤的其他問題

androidjsonretrofit2