繁体   English   中英

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

[英]How to use Stetho with okhttp3 for network inspection?

我启动了Android模拟器(Genymotion)应用。 问题是我无法检查Chrome开发工具中的任何内容(请参见下面的屏幕截图)。 有小费吗 ?

Stetho

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

这是我在Chrome chrome:// inspect /中输入此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

为了解决这个问题,我认为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>

在此处输入图片说明

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

使用Facebook的Stetho调试Android应用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM