facebook / stetho

Stetho is a debug bridge for Android applications, enabling the powerful Chrome Developer Tools and much more.
http://facebook.github.io/stetho/
MIT License
12.68k stars 1.13k forks source link

Network inspection for OkHttp + Retrofit issue #36

Closed antonkrasov closed 9 years ago

antonkrasov commented 9 years ago

Hi Guys,

First of all, thanks for your job, great project! I'm trying to setup Stetho, but I don't see any network calls in Network tab.

Here is my OkHttpClient initialization:

...
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());

Then I create RestAdapter with this client

return new RestAdapter.Builder()
                .setEndpoint(endpoint)
                .setClient(new OkClient(okHttpClient))
                ...

I use 2.2.0:

compile 'com.squareup.okhttp:okhttp:2.2.0'

This is in my Application class:

Stetho.initialize(
                Stetho.newInitializerBuilder(this)
                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                        .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
                        .build());

If I open console I see "Powered by Stetho", but I don't see any network calls. But Retrofit display full logs fine.

What I'm doing wrong? :) Thanks.

fingertricks commented 9 years ago

You need to pass the client (with interceptors) into the RestAdapter Builder: setClient(new OkClient(okHttpClient)) should read: setClient(client)

antonkrasov commented 9 years ago

@fingertricks, that's exactly how it works. I just use dagger, so client just returned from @Provides method. And Injects as okHttpClient. I can't use setClient(client), as Retrofit requires OkClient, inside it I can pass my custom OkHttpClient.

jasta commented 9 years ago

Are you using Retrofit <= 1.8? According to https://github.com/square/okhttp/wiki/Interceptors, it won't work on that older version without some additional effort.

Tremelune commented 9 years ago

The Interceptors he is using were not available in Retrofit 1.8.

I'm having the same issue. In the logs I see:

W/System.err﹕ java.io.IOException: Address already in use W/System.err﹕ at android.net.LocalSocketImpl.bindLocal(Native Method) W/System.err﹕ at android.net.LocalSocketImpl.bind(LocalSocketImpl.java:303) W/System.err﹕ at android.net.LocalServerSocket.(LocalServerSocket.java:52) W/System.err﹕ at com.facebook.stetho.server.LocalSocketHttpServer.bindToSocket(LocalSocketHttpServer.java:278) W/System.err﹕ at com.facebook.stetho.server.LocalSocketHttpServer.listenOnAddress(LocalSocketHttpServer.java:138) W/System.err﹕ at com.facebook.stetho.server.LocalSocketHttpServer.run(LocalSocketHttpServer.java:134) W/System.err﹕ at com.facebook.stetho.Stetho$1.run(Stetho.java:80) E/stetho﹕ Could not bind to socket.

jasta commented 9 years ago

@antonkrasov let me spin up a quick Retrofit 1.9 + OkHttp 2.2.0 sample app to see if I can debug through the problems locally.

@Tremelune that error is unrelated (but still a bug). It's a harmless issue but is unintentionally noisy. I'll fix it soon.

antonkrasov commented 9 years ago

@jasta wow, thanks! I really forgot to update Retrofit, updated OkHttp to 2.2.0, but continue using Retrofit 1.7.0

Tremelune commented 9 years ago

Same issue, with OkHttp 2.2.0 and Retrofit 1.9.

antonkrasov commented 9 years ago

@Tremelune, seems like I need to reopen then.

jasta commented 9 years ago

@Tremelune I tested this locally and cannot see an issue with Retrofit 1.9. Can you try adding the logging interceptor mentioned in https://github.com/square/okhttp/wiki/Interceptors. I'm curious if that is working. If no, I think your issue is just with interceptors in general.

Tremelune commented 9 years ago

Yup, I'll see what I can do. I'm hoping for the ol' "clean build" fix...

antonkrasov commented 9 years ago

Updating Retrofit to 1.9.0 didn't help, trying to add logging interceptor.

antonkrasov commented 9 years ago

@jasta LoggingInterceptor from https://github.com/square/okhttp/wiki/Interceptors works fine for me. But Network tab still doesn't show anything.

jasta commented 9 years ago

I confirmed my sample app is working correctly with Retrofit 1.9 and OkHttp 2.2.0. Source is here: https://github.com/jasta/retrofit-okhttp-stetho-demo

Here's the modified GitHubClient: https://github.com/jasta/retrofit-okhttp-stetho-demo/blob/master/app/src/main/java/com/example/retrofit/GitHubClient.java

Screenshot to prove it :) retrofit

antonkrasov commented 9 years ago

@jasta cloning your demo app :) will check it now

antonkrasov commented 9 years ago

@jasta yeah, your demo app works for me. Hmm, let me compare with my project and try to find the reason.

antonkrasov commented 9 years ago

@jasta Wooohoo, thank you :) I found the issue, and it's quite simple: Proguard.

I fixed this for my project by adding:

-keep class com.facebook.stetho.**{ *; }
vandyshev commented 9 years ago
W/System.err﹕ java.io.IOException: Address already in use
W/System.err﹕ at android.net.LocalSocketImpl.bindLocal(Native Method)
W/System.err﹕ at android.net.LocalSocketImpl.bind(LocalSocketImpl.java:254)
W/System.err﹕ at android.net.LocalServerSocket.<init>(LocalServerSocket.java:52)
W/System.err﹕ at com.facebook.stetho.server.LocalSocketHttpServer.bindToSocket(LocalSocketHttpServer.java:278)
W/System.err﹕ at com.facebook.stetho.server.LocalSocketHttpServer.listenOnAddress(LocalSocketHttpServer.java:138)
W/System.err﹕ at com.facebook.stetho.server.LocalSocketHttpServer.run(LocalSocketHttpServer.java:134)
W/System.err﹕ at com.facebook.stetho.Stetho$1.run(Stetho.java:80)

Just run https://github.com/jasta/retrofit-okhttp-stetho-demo