如何使用 Stetho 和 okhttp3 进行网络检查?

How to use Stetho with okhttp3 for network inspection?

我在 Android 模拟器 (Genymotion) 上启动了应用程序。问题是我无法检查 Chrome Dev Tools 中的任何内容(请参见下面的屏幕截图)。有什么建议吗?

Stetho

Google Chrome 开发工具在(地址栏)

这是我在 Chrome 中输入这个 URL 后看到的 chrome://检查/

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(应用程序)

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

为了解决这个问题,我认为应用程序 class 应该在应用程序包中而不是在 java 包中。此外,确保应用程序 class 在

中声明

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>

有关如何使用 Stetho 的更多信息,我推荐此教程。

Debugging Android Apps with Facebook's Stetho