4

I'm using com.squareup.retrofit2:retrofit:2.2.0

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(logging);


    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://localhost:9000")
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();

    final UserService service = retrofit.create(UserService.class);

    Call<User> userCall = service.createUser(user)

Here is the problem: when I run the execute it make REST API request but when I use enqueue it does nothing (no exception, no log)

  userCall.execute(); //this work

  //this does not work
  userCall.enqueue(new Callback<User>() {
      @Override
      public void onResponse(Call<User> call, Response<User> response) {
          // no response 
      }

      @Override
      public void onFailure(Call<User> call, Throwable t) {
          //nothing here
      }
  });
Mujtaba Alboori
  • 395
  • 1
  • 7
  • 20

4 Answers4

5

Well,

The issue was because I was using an emulator and try to call the localhost however, it turn out if you want call the localhost you have to use this ip 10.0.2.2

The execute works because I was running as unit test but when it runs enqueue I think it need to run on android platform.

refer to this https://developer.android.com/studio/run/emulator.html#networkaddresses

How to get the Android Emulator's IP address?

Community
  • 1
  • 1
Mujtaba Alboori
  • 395
  • 1
  • 7
  • 20
  • In case you want to run your web-services on a localhost .You can always define ip to redirec via modifying host file of emulator.That is well to go option without any hassles. – DevKRos Mar 31 '17 at 07:09
1

You can try this full Answer Link

Here is the shortest code snippest.

    private void emailLoginRequest() {
    LoginService loginService = ApiFactory.createService(LoginService.class);
    Call<JsonObject> call = loginService.login(edtEmail.getText().toString(),edtPassword.getText().toString(),mDeviceType,mDeviceToken);
    call.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            hideProgressDialog();
            if (response.isSuccessful()) {
                LOGD(TAG, "onResponse 0: " + response.body().toString());
                LoginResponse loginResponse = new Gson().fromJson(response.body().toString(), LoginResponse.class);

                System.out.println("+++ get message >> " + loginResponse.getMessage());
                int status = loginResponse.getStatus();

            }else {
                LOGD(TAG, "response fail 0: " + response.body());
            }
        }

        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            hideProgressDialog();
            LOGD(TAG, "onFailure: " + t.getMessage());
        }
    });
}
Community
  • 1
  • 1
Roadies
  • 3,309
  • 2
  • 30
  • 46
0

put your code as this:

 userCall.enqueue(new Callback<User>() {
    @Override
    public void onResponse(Call<User> call, Response<User> response) {
        Toast.makeText(this, "in response", Toast.LENGTH_SHORT).show(); 
    }

    @Override
    public void onFailure(Call<User> call, Throwable t) {

        Toast.makeText(this, "in response", Toast.LENGTH_SHORT).show(); 
    }
});

Now based on method call you will get toast if its in response toast will be "In response" don't forget write youractivity name before this like : MainActivity.this

DevKRos
  • 422
  • 2
  • 15
0

see this

retrofit can be called by 2 methods separately one is synchronous and another way is asynchronous.

enqueue is an asynchronous way where it does not wait for a response and proceed. here you need to handle Call separately to load UI later.

Execute is asynchronously call rest api and wait until get response and process it.

choice wisely

Umesh Bhutada
  • 301
  • 2
  • 4