Retrofit2无法调用onResponse和onFailure吗?

时间:2018-07-16 07:26:36

标签: android retrofit2

我正在尝试将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 ;
    }





}


}

在这里我正在尝试在Response和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
                        );

4 个答案:

答案 0 :(得分:2)

尝试:

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){
            }
    });

答案 1 :(得分:0)

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

公共无效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();
                }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.

}

答案 2 :(得分:0)

您是否正在调用此函数send_info()..如果已调用,请通过调试进行检查。

答案 3 :(得分:0)

代码中没有错误,唯一的问题是响应主体存在差异。这就是我应该作为响应检索的内容

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

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

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