charlesfranciscodev charlesfranciscodev - 5 months ago 40
Android Question

How to use Stetho with okhttp3 for network inspection?

I launched the app the Android emulator (Genymotion). Problem is I cannot inspect anything in Chrome Dev Tools (see screenshot below). Any tips ?

Stetho

Google Chrome Dev Tools in (address bar)

This is what I see after entering this URL in Chrome
chrome://inspect/

Chrome Dev Tools

TrainerWorkoutApplication.java

import android.app.Application;
import android.content.Context;

import com.facebook.stetho.Stetho;

public class TrainerWorkoutApplication extends Application {
@Override
public void onCreate() {
super.onCreate();

Stetho.initialize(Stetho.newInitializerBuilder(this)
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build());
} // onCreate()

} // TrainerWorkoutApplication


LogInActivity.java

package com.trainerworkout.trainerworkout.activity;

//...

/** As a personal trainer I need to log in so that I can have access to the app. */
public class LogInActivity extends AppCompatActivity {
public static OkHttpClient client;
private OkHttpClient.Builder builder = new OkHttpClient.Builder();

@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
builder.addInterceptor(new AddCookiesInterceptor(context));
builder.addInterceptor(new ReceivedCookiesInterceptor(context));

builder.addNetworkInterceptor(new StethoInterceptor());

client = builder.build();
} // onCreate()

//...

} // LogInActivity


build.gradle (app)

apply plugin: 'com.android.application'

android {
//...
} // android

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
compile 'com.google.code.gson:gson:2.5'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'de.hdodenhof:circleimageview:2.0.0'
compile 'com.facebook.stetho:stetho:1.3.1'
compile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
} // dependencies

Answer

To fix this, I think the Application class should be in the application package and not in the java package. Also, make sure the application class is declared in the

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.trainerworkout.trainerworkout"
          xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <application
        ...
        android:name=".TrainerWorkout">
        ...
    </application>
</manifest>

enter image description here

For more info on how to use Stetho I recommend this tutorial.

Debugging Android Apps with Facebook's Stetho