如何使用带有okhttp3的Stetho进行网络检查?

时间:2016-05-19 17:22:13

标签: java android okhttp3 stetho

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

Stetho

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

这是我在Chrome中输入此网址后看到的内容 铬://检查/

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

1 个答案:

答案 0 :(得分:0)

要解决这个问题,我认为Application类应该在应用程序包中,而不是在java包中。另外,确保在

中声明了应用程序类

的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

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

Debugging Android Apps with Facebook's Stetho

相关问题