neustart47 neustart47 - 1 year ago 482
Java Question

Retrofit: How to wait for response

I have AsyncTask and the doInBackground method inside which, I sending POST request using Retrofit. My code looks like:

//method of AsyncTask
protected Boolean doInBackground(Void... params) {
Retrofit restAdapter = new Retrofit.Builder()
IConstructSecureAPI service = restAdapter.create(IConstructSecureAPI.class);
Call<JsonElement> result = service.getToken("TestUser", "pass", "password");
result.enqueue(new Callback<JsonElement>() {
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {


public void onFailure(Call<JsonElement> call, Throwable t) {


return true;

My problem is following: Retrofit sending request asynchronously and while it doing it, the doInBackground method returning value. So I need to sending a request in the same thread, that all execution in the sequence. One by one. And returning from doInBackground occurs after request finished. How can I send a request in the same thread using Retrofit?

Answer Source

The Call class has an execute() method that will make your call synchronously.

enqueue() is explicitly for making an asychronous call.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download