Retrofit call enqueue doesn't reach onResponse or onFailure

I know this is a common question, but I did all of them, still no resolved. In my MainActivity, I had a call from an ServerService.java, like this:

String randomNumber = serverService.contactServer();

In the ServerService.java, the contactServer() will call the method which contains the .enqueue:

  public String contactServer() {     
    return requestServerService();
}

And the requestServerService() contains the code:

 public String requestServerService() {

    Call<RequestAttributes> call = new RetrofitConfig().getServiceRequester().requestRandomNumber();

    call.enqueue(new Callback<RequestAttributes>() {
        @Override
        public void onResponse(Call<RequestAttributes> call, Response<RequestAttributes> response) {
            if (!response.isSuccessful()) {
                Log.i("Err", "Err: " + response.code());
            } else {
                RequestAttributes requestAttributes = response.body();
                returnedValue = requestAttributes.getRandomNumber();
                Log.d("jsonAnswer", "O numero aleatorio é: " + returnedValue);
            }
        }

        @Override
        public void onFailure(Call<RequestAttributes> call, Throwable t) {
            Log.e("Fail", "Failed: " + t.getMessage());
        }
    }); return returnedValue;

The error is the returnedValue returns null. I tried debbuging, but even it doesn't reach onReponse. I know the problem must be because .enqueue is asynchronous, but how can I resolve this problem and return the request to the mainActivity?

The config of Retrofit:

  public RetrofitConfig() {

    this.retrofit = new Retrofit.Builder()
                    .baseUrl("localhost:3000/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

}

public ServiceRequester getServiceRequester() {
    return this.retrofit.create(ServiceRequester.class);
}

The POJO:

public class RequestAttributes {


@SerializedName("randomNumber")
private String randomNumber;

public String getRandomNumber() {
    return randomNumber;
}

public void setRandomNumber(String randomNumber) {
    this.randomNumber = randomNumber;
}

@Override
public String toString() {
    return "RequestAttributes{" +     
            ", randomNumber='" + randomNumber + '\'' +
            '}';
}

}

And the request:

 @GET("api/requestRandomNumber")
 Call<RequestAttributes> requestRandomNumber();

The JSON answer if I request via browser:

{"randomNumber":"u845gq"}

Upvotes: 1

Views: 1384

Answers (1)

Ibrahim Disouki
Ibrahim Disouki

Reputation: 2693

You can pass callbacks from your MainActivity to contactServer() method

        serverService.contactServer(new Callback<RequestAttributes>() {
        @Override
        public void onResponse(Call<RequestAttributes> call, Response<RequestAttributes> response) {
            if (!response.isSuccessful()) {
                Log.i("Err", "Err: " + response.code());
            } else {
                RequestAttributes requestAttributes = response.body();
                String returnedValue = requestAttributes.getRandomNumber();
                // Do what you want here with returnedValue. You are in the activity thread(MainThread or UIThread) for example someTextView.setText(returnedValue);
                Log.d("jsonAnswer", "O numero aleatorio é: " + returnedValue);
            }
        }

        @Override
        public void onFailure(Call<RequestAttributes> call, Throwable t) {
            Log.e("Fail", "Failed: " + t.getMessage());
        }
    });

Then make it void method, pass the callback to requestServerService() method

public void contactServer(Callback<RequestAttributes> callback) {
    requestServerService(callback);
}

Then implement requestServerService() method like this:

public void requestServerService(Callback<RequestAttributes> callback) {

    Call<RequestAttributes> call = new RetrofitConfig().getServiceRequester().requestRandomNumber();

    call.enqueue(callback);
}

Upvotes: 1

Related Questions