Error during different retrofit calls in the same model with different variables

advertisements

I'm working with retrofit and I have some problem that I don't know how can I fix it. I have two methods to call my api. One is working fine that is method smsPincodeCheck.

My problem is right now when I try to use another api call. When I try to add isValidPincode any of the two methods are working.

I think that the problem must be in my model class, because when I add the new constructor with three variables and @SerializedName("pincode") don't work any of the two calls.

smsPincodeCheck send to the server two variables isValidPincode send to the server three variables

To can used it, I've created two constructores in the model, one to can use it with two variables and another one with three. I'm suspecting that the problem could be in this place, but I'm not sure how can fix it.

Some help will be appreciate.

Interface:

 @POST("api/check")
    Call<SMSPinCode> smsPincodeCheck(@Body User user);

    @POST("api/check/code")
    Call<ResponseSMS> isValidPincode(@Body User user);

Model:

    @Expose
    private Long id;
    private String name;
    private String lastname;
    @SerializedName("email")
    private String email;
    @SerializedName("phoneNumber")
    private String telephone;
    @SerializedName("pincode")
    private String pincode;

 public User(String phone, String email){
        this.telephone = phone;
        this.email = email;
    }

    public User(String phone, String email, String pincode){
        this.telephone = phone;
        this.email = email;
        this.pincode = pincode;
    }

//Getter and setters

My Two methods: 1) Call isValidPincode:

 User user = new User("34649375362", "[email protected]", "50878");
        Call<ResponseSMS> call = apiService.isValidPincode(user);
        call.enqueue(new Callback<ResponseSMS>() {
            @Override
            public void onResponse(Call<ResponseSMS> call, Response<ResponseSMS> response) {
                if (response.isSuccess()) {
                    Toast.makeText(RegisterActivity.this, "200", Toast.LENGTH_SHORT).show();
                    Log.i(TAG, "Pincode Check: " + response.code());
                }
                if (response.code() == 400) {
                    Toast.makeText(RegisterActivity.this, "400", Toast.LENGTH_SHORT).show();
                    Log.i(TAG, "Pincode Check: " + response.code());
                }
            }
            @Override
            public void onFailure(Call<ResponseSMS> call, Throwable t) {
                Log.i(TAG, call.request().body().toString());
                Log.i(TAG, t.getMessage());

            }
        });

2) smsPinCodeCheck:

 user = new User("34649375362", "[email protected]");
        Call<ResponseSMS> call = apiService.smsPincodeCheck(user);
        progressDialog = ProgressDialog.show(RegisterThreeActivity.this, "", getResources().getString(R.string.loading));
        call.enqueue(new Callback<ResponseSMS>() {
            @Override
            public void onResponse(Call<ResponseSMS> call, Response<ResponseSMS> response) {
                if(response.isSuccess()){
                    Toast.makeText(RegisterThreeActivity.this, "200", Toast.LENGTH_SHORT).show();
                    Log.i(TAG, "Pincode Check: " + response.code());
                }

                if(response.code() == 401){
                    sendSMS();
                    Log.i(TAG, "Pincode Check: " + response.code());
                }
            }

            @Override
            public void onFailure(Call<ResponseSMS> call, Throwable t) {
                progressDialog.dismiss();
                Toast.makeText(RegisterThreeActivity.this, "Failure check sms", Toast.LENGTH_SHORT).show();
            }
        });


Enable logging in your retrofit api. It'll log everything that its sending to the server and the response.

private void enableLogging(OkHttpClient.Builder builder) {
    if (builder != null) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        // Retrofit2 okhttp client is now a builder
        builder.addInterceptor(logging);
    }
}

That way you can debug what is being sent to the server and what is missing.

For more details: Retrofit Logging

Its not really an answer but this can help you out.