arsalan
arsalan

Reputation: 25

Retrofit2 cannot call onResponse and onFailure?

I am trying to post json data to server but the call.enqueue onResponse and onFailure are not called.

This is the type of Json data that i want to post to the server.

{"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"

}

]} 

this is the modal class of my data.

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





}


}

in activity here i am trying to call on Response and on Failure in callx.enqueue I have recieve the data in these variables.

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.

}





}

in Api Interface

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

Upvotes: 0

Views: 1225

Answers (4)

arsalan
arsalan

Reputation: 25

There was no error in code the only thing was the there was difference in response body. This i what i should have retrieved as response

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

instead i was trying to get this as response.body.GetMsg

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

Upvotes: 0

user8484700
user8484700

Reputation: 11

Are you calling this function send_info() ..If called then check by debugging .

Upvotes: 0

Baqer Shah
Baqer Shah

Reputation: 101

try:

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

Upvotes: 2

Paramasivam Mathesh
Paramasivam Mathesh

Reputation: 46

first print the value in logcat. After check the logcat to find the error

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

}

Upvotes: 0

Related Questions