square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.12k stars 7.3k forks source link

Enqueue callback not called #1693

Closed shilch closed 8 years ago

shilch commented 8 years ago

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!

swankjesse commented 8 years ago

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

JakeWharton commented 8 years ago

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 notifications@github.com 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

shilch commented 8 years ago

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.

shilch commented 8 years ago

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 ...
tristangrichard commented 8 years ago

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.

shilch commented 8 years ago

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

shilch commented 8 years ago

Solved. Used synchronize where there should not be one

FranPR9 commented 8 years ago

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

shilch commented 8 years ago

@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.

tejasuplenchwar commented 7 years ago

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

tejasuplenchwar commented 7 years ago

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

LunaticMeta commented 6 years ago

i have same problem too :(

guilmarc commented 6 years ago

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

joelc1225 commented 6 years ago

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.

udayanem commented 5 years ago

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?

joelc1225 commented 5 years ago

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.

TheLe0 commented 3 years ago

I had the same problem these days, i solved inserting this line on my AndroidManifest.xml>

<uses-permission android:name="android.permission.INTERNET" />

In case somebody else still needs...

Namida2 commented 2 years ago

I replaced enqueue with execute and it worked for me