【问题标题】:Retrofit2 cannot call onResponse and onFailure?Retrofit2 不能调用 onResponse 和 onFailure?
【发布时间】:2018-07-16 07:26:35
【问题描述】:

我正在尝试将 json 数据发布到服务器,但未调用 call.enqueue onResponse 和 onFailure。

这是我要发布到服务器的 Json 数据类型。

{"ClientId":16,
"CityId":1,
"TotalCheque":2,



"Cheques":[
 {
  "ChequeAmount": 15000,
  "ChequeNumber":205,
   "ChequeDate": "11/7/18",
   "ChequeImage":"www/ass/img10111"

},

 {
  "ChequeAmount": 21000,
  "ChequeNumber":297,
   "ChequeDate": "14/7/18",
   "ChequeImage":"www/ass/img10111"

}

]} 

这是我的数据的模态类。

public class AllCheques {
    @SerializedName("ClientId")
@Expose
public Integer clientid;
@SerializedName("CityId")
@Expose
public Integer cityid;
@SerializedName("TotalCheque")
@Expose
public Integer totalcheque;

@SerializedName("Cheques")
@Expose
public ArrayList<Cheque_Payment> cheques;


public AllCheques(Integer cityid , Integer clientid , Integer totalcheque ,ArrayList<cheques> chequesall){
    this.cityid = cityid;
    this.clientid = clientid;
    this.totalcheque =totalcheque;
    this.chequesall = chequesall;

}

public Integer getCityId() {
    return cityid ;
}

public void setCityId(Integer cityid ) {
    this.cityid = cityid;
}

public Integer getClientId() {
    return clientid;
}

public void setClientId(Integer clientid) {
    this.clientid = clientid;
}

public Integer getTotalcheque() {
    return totalcheque ;
}

public void setTotalcheque(Integer totalcheque) {
    this.totalcheque = totalcheque;
}


public  ArrayList<cheques> chequesall = new ArrayList<>();

public ArrayList<cheques> getChequesall() {
    return chequesall;
}

public void setChequesall(ArrayList<cheques> chequesall )
{
    this.chequesall = chequesall;
}



public static class cheques{

    @SerializedName("ChequeAmount")
    @Expose
    public Integer chequeAmount;
    @SerializedName("ChequeNumber")
    @Expose
    public Integer chequeNumber;
    @SerializedName("ChequeDate")
    @Expose
    public String chequeDate;

    @SerializedName("ChequeImage")
    @Expose
    public String chequeImage;


    public cheques( Integer chequeAmount , Integer chequeNumber ,String chequeDate, String chequeImage)
    {
        this.chequeAmount = chequeAmount;
        this.chequeNumber = chequeNumber;
        this.chequeDate = chequeDate;
        this.chequeImage = chequeImage;

    }

    public cheques() {

    }


    public Integer getChequeAmount() {
        return chequeAmount ;
    }

    public void setChequeAmount(Integer chequeAmount) {
        this.chequeAmount =chequeAmount;
    }

    public Integer getChequeNumber() {
        return chequeNumber ;
    }

    public void setChequeNumber(Integer chequeNumber) {
        this.chequeNumber =chequeNumber;
    }

    public String getChequeDate() {
        return chequeDate ;
    }

    public void setChequeImage(String chequeImage) {
        this.chequeImage =chequeImage;
    }

    public String getChequeImage() {
        return chequeImage ;
    }





}


}

在这里的活动中,我试图在 callx.enqueue 中调用响应和失败 我已收到这些变量中的数据。

public void send_info()
{

CityID = Integer.parseInt(selectedId);
        ClientID = Integer.parseInt(cclient_id);
        TotalCheques = val;
        addcheque_tolist();
//this give chequeslist

 //here i get all data now calling
ApiInterface = ApiClient.getApiClient().create(Api.class);
        Call<CHQ> callx = ApiInterface.SaveCheques(CityID,ClientID,TotalCheques,chequeslist);
        callx.enqueue(new Callback<CHQ>() {
            @Override
        public void onResponse(Call<CHQ> callx, Response<CHQ> response) {
            if (response.isSuccessful()) {
                String data = response.body().toString();
                Toast.makeText(Recieve_Payment.this, response.body().getMsg().toString(), Toast.LENGTH_LONG).show();
                }
        }

        @Override
        public void onFailure(Call<CHQ> callx, Throwable t) {

        }
    });

//here on response and on failure does not debug or pass.

}





}

在 Api 接口中

@FormUrlEncoded
@POST("dummy_chequedata.php")
Call<CHQ> SaveCheques(@Field("clientid") Integer ClientID,
                          @Field("cityid") Integer CityID,
                          @Field("totalcheque") Integer TotalCheques,
                          @Field("chequeslist[]")ArrayList<AllCheques.cheques> chequeslist
                        );

【问题讨论】:

  • 你有没有在你的 onFailure 中放任何日志或任何东西?
  • 在 onresponse 或失败中没有得到任何日志它没有进入 onresponse 或 onfailure 直接结束该方法。

标签: android retrofit2


【解决方案1】:

试试:

ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    Call<CHQ> callx = apiInterface.SaveCheques(CityID, ClientID, TotalCheques, chequeslist);
        callx.enqueue(new Callback<CHQ>(){
            @Override
            public void onResponse(Call<CHQ> callx,Response<CHQ> response){
                if(response.isSuccessful()){
                    String data=response.body().toString();
                    Toast.makeText(Recieve_Payment.this,response.body().getMsg().toString(),Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailure(Call<CHQ> callx,Throwable t){
            }
    });

【讨论】:

  • 我认为问题在于我希望我的响应正文显示消息,即{code=1,msg="OK"} 但从服务器它将获取数据以显示在响应正文中,例如数据:{cityid:1,clientid:13}
  • 你能解决这个问题吗?如果没有,那么我将与您分享完整的工作代码......
【解决方案2】:

首先打印 logcat 中的值。检查logcat后发现错误

公共无效发送信息() {

CityID = Integer.parseInt(selectedId);
        ClientID = Integer.parseInt(cclient_id);
        TotalCheques = val;
        addcheque_tolist();
//this give chequeslist

 //here i get all data now calling
ApiInterface = ApiClient.getApiClient().create(Api.class);
        Call<CHQ> callx = ApiInterface.SaveCheques(CityID,ClientID,TotalCheques,chequeslist);
        callx.enqueue(new Callback<CHQ>() {
            @Override
        public void onResponse(Call<CHQ> callx, Response<CHQ> response) {
            if (response.isSuccessful()) {
                String data = response.body().toString();
                Toast.makeText(Recieve_Payment.this, response.body().getMsg().toString(), Toast.LENGTH_LONG).show();
                }else{
                     Log.e("response:","response failiure");
        }

        @Override
        public void onFailure(Call<CHQ> callx, Throwable t) {
                     Log.e("response:",t.getMessage);
        }
    });

//here on response and on failure does not debug or pass.

}

【讨论】:

  • onResponse 和 onFailure 永远不会被调用,因此也不会显示任何日志。我已经这样做了
  • 这里有什么?获取 API 客户端()。另外,不能没有日志,尝试干净重建。
  • 请分享截图。请检查 logcat 中的包名
【解决方案3】:

你是否调用了这个函数 send_info() ..如果调用了,那么通过调试检查。

【讨论】:

  • 在 -- if(response.isSuccessful()){ 和 On Failure 下标记调试断点 .. 并检查它是否正在调用 .. 你应该在 menifest.xml 中有 Internet 权限跨度>
【解决方案4】:

代码没有错误,唯一的问题是响应正文有所不同。这是我应该作为响应检索到的内容

{"data:{"CheuqueNumber":"123123","TotalCheque":"4", "Cheque1Amount":"90000","cityid":"521","clientid":"16"}}

相反,我试图将其作为 response.body.GetMsg

{code:"1",msg:"OK"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多