Retrofit: Enqueue callback not called

Created on 23 Mar 2016  Â·  16Comments  Â·  Source: square/retrofit

Hi,

I am using retrofit2 and recognized that sometimes the callback for Call.enqueue is not called.
e.g.

mService.getItem("myID").enqueue(new retrofit2.Callback<Item>() {
    @Override
    public void onResponse(Call<Item> call, Response<Item> response) {
        Log.d(TAG, "onResponse()");
    }

    @Override
    public void onFailure(Call<Item> call, Throwable t) {
        Log.d(TAG, "onFailure()", t);
    }
});

I do not get a message in the log and also no timeout error.
Any ideas why this happens?

Thank you in advance!

Most helpful comment

What solved it for me was observing the result of the call in my activity using LiveData.

So after enqueue gets called on a background thread, that call may take a few seconds to return. So the code that runs after enqueue was trying to already access the data before the data had returned from the API.

So I removed all log statements outside of the enqueue method, so I'm not trying to access any null data before it returned from the API, and instead I'm observing the result of the call in my activity class using the LiveData 'observe' method. My enqueue call is in the 'NetworkDataSource' class of my 'AndroidHub' project, if you wanna check it out.

All 16 comments

Are you sure you see Log.d() logs?

Also, add the OkHttp logging interceptor and see what the HTTP client is
actually doing.

On Wed, Mar 23, 2016, 8:13 PM Jesse Wilson [email protected] wrote:

Are you sure you see Log.d() logs?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/square/retrofit/issues/1693#issuecomment-200592032

Yes, I see logs. This is what I do:

private static final String TAG = Fooclass.class.getSimpleName();

public void execute(){
    Log.d(TAG, "execute()");
    mService.getItem("123").enqueue(new Callback<Item>() {
        @Override
        public void onResponse(Call<Item> call, Response<Item> response) {
            Log.d(TAG, "onResponse()");
        }

        @Override
        public void onFailure(Call<Item> call, Throwable t) {
            Log.d(TAG, "onFailure()", t);
        }
    });
}
OkHttpClient client = new OkHttpClient.Builder()
        // this is my interceptor to set the authorization header
        .addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
                        .addHeader("Accept", "application/json")
                        .addHeader("Authorization", token.getType() + " " + token.getToken())
                        .build();
                return chain.proceed(request);
            }
        })
        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
        .protocols(Arrays.asList(/*Protocol.HTTP_2, */Protocol.HTTP_1_1))
        .build();
builder.client(client);

And in the Logcat I type Fooclass|OkHttp:

D/Fooclass: execute()
D/OkHttp: --> GET https://example.com/api/v1/items/123 http/1.1
200 OK https://example.com/api/v1/items/123 (131ms, 153-byte body)

No Log from Callback is called, no timeout.

Using

com.squareup.retrofit2:retrofit:2.0.0-beta4

instead of

com.squareup.retrofit2:retrofit:2.0.0

makes no difference.
When using

com.squareup.retrofit2:converter-gson:2.0.0-beta4

instead of

com.squareup.retrofit2:converter-gson:2.0.0

I get

com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at ...

2.0.0 is the latest release.
It sounds like your "Item" object does not have the correct structure compared to the JSON object you are receiving.
Try to use Postman or httplogging to see what the api returns.

The item has the right structure. When using JacksonConverter instead of GsonGonverter, it works. But the callback is still not called.

Solved. Used synchronize where there should not be one

@shilch could you be more specific please, i'am having the same problem.

@FranPR9 Sorry, this issue is 5 month ago and I forgot it already. Be sure that there is no synchronize that can block you requests.

on retrofit POST method my callbacks are not working any one have solution for this

I am using Retrofit2
If i am using post to send data to server can not able to get response from server
means my onResponse and onFailure not call

i have same problem too :(

Same problem here, no solution on this forum, dam !

same problem here. any help would be appreciated. I'm trying to use enqueue method inside a non-activity class using repository pattern. Repository calls the NetworkDatasource class which contains the Retrofit api call. When debugging, the code skips over the enqueue callbacks.

I am also having the same problem. I have log statements for both onResoponse() and onFailute() methods, but nothing executed. Control moves just after the enque() method. I am getting proper data in Postman, but i am not getting proper response in code when i am invoking through Retrofit. Why nobody is looking into this issue, as many people are still stuck with this issue. Is there something we all are missing?

What solved it for me was observing the result of the call in my activity using LiveData.

So after enqueue gets called on a background thread, that call may take a few seconds to return. So the code that runs after enqueue was trying to already access the data before the data had returned from the API.

So I removed all log statements outside of the enqueue method, so I'm not trying to access any null data before it returned from the API, and instead I'm observing the result of the call in my activity class using the LiveData 'observe' method. My enqueue call is in the 'NetworkDataSource' class of my 'AndroidHub' project, if you wanna check it out.

Was this page helpful?
0 / 5 - 0 ratings