1

I'm having this code. my input data (Call process) successfully inserted to the database, but I didn't get the return data using call.enqueue. it gives me the "failed" toast instead the "success" one. I'm using retrofit 2.1.0. and here is my imports:

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

and this is my code

public void performRegistration(){
    final String name = uname.getText().toString();
    final String email = mail.getText().toString();
    final String pw1 = password1.getText().toString();
    final String pw2 = password2.getText().toString();

    Call<User> call = MainActivity.apiInterface.performRegistration(name, email, pw1);

    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            MainActivity.prefConfig.displayToast("success");
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            MainActivity.prefConfig.displayToast("Failed");
        }
    });
}
Shashanth
  • 4,995
  • 7
  • 41
  • 51
xbee
  • 161
  • 1
  • 8

2 Answers2

0

onResponse handle all the response from server. when you get status code 404 or 500, this method will be called. you can check status code using response.code(). onFailure invoked when your application can't connect to the server.

Jalalkun
  • 181
  • 1
  • 8
  • i have the internet connection. my first call successfully send the data. and the return code is correct from my server.. so where do i have to put the response.code() ? – xbee Oct 01 '18 at 05:05
  • in onResponse(), or you can use this code for display all response `new Gson().toJson(response)` – Jalalkun Oct 01 '18 at 07:17
0

This may happened due to some reasons . 1)No Internet available. 2)Can't connect to the server. Please Post your proper response error by using e.printstack(); Thanks..

Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30