如何使用神器Stetho调试Volley

1,017 阅读2分钟

Stetho简介

  Stetho 是 Facebook 开源的一个 Android 调试工具。是一个 Chrome Developer Tools 的扩展,可用来检测应用的网络、数据库、WebKit 等方面的功能。开发者也可通过它的 dumpapp 工具提供强大的命令行接口来访问应用内部。无需root查看sqlite文件、sharedpreference文件等等。更多详细介绍可以进入Stetho官网

Stetho结合OkHttp使用

添加依赖
// Gradle dependency on Stetho 
  dependencies { 
    compile 'com.facebook.stetho:stetho:1.1.1' 
  }
Stetho初始化配置

在App的Application中完成初始化。

public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    Stetho.initialize(
      Stetho.newInitializerBuilder(this)
        .enableDumpapp(
            Stetho.defaultDumperPluginsProvider(this))
        .enableWebKitInspector(
            Stetho.defaultInspectorModulesProvider(this))
        .build());
  }
}

官网中使用OkHttp为实例,使用如下

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

然后就可以运行App进行调试,基本上可以满足调试需求了。

Stetho结合Volley使用

官网中Stetho是结合OkHttp的使用,如果项目中使用Volley做为网络请求框架,可以做如下修改。还是使用OkHttp做为Volley中HttpStack的实现,我们知道,Volley中网络请求在Android2.3及以上基于HttpURLConnection,2.3以下基于HttpClient实现,通过增加HttpStack的具体实现即可。这里使用Bryan Stern分享的代码。(网页可能被墙,可以通过VPN访问。需要VPN的可以点击这里

添加依赖
compile 'com.facebook.stetho:stetho:1.1.1'
compile 'com.facebook.stetho:stetho-okhttp:1.1.1'
compile 'com.squareup.okhttp:okhttp:2.3.0'
Stetho初始化配置
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());
mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new OkHttpStack(client));

好了,基本上这样就能使用Stetho神器调试你的App了,感觉到强大了么~。

补充:使用中遇到的坑

  • Stetho inspect窗口空白

    如果出现调试窗口空白,先升级下Chrome吧。升级最新版后再试一下(我被这个坑了)。

  • Stetho inspect窗口还是空白

    如果Chrome是最新版,无论如何刷新都是空白,那么恭喜你你可能被墙了~用VPN试试吧 可以戳这里哦

我的测试代码和效果图如下:

自定义Application类:

public class MyAppliation extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        instance = this;

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

    /**
     * @return The Volley Request queue
     */
    public RequestQueue getRequestQueue() {
        // lazy initialize the request queue, the queue instance will be
        // created when it is accessed for the first time
        synchronized (App.class) {
            if (mRequestQueue == null) {
                OkHttpClient client = new OkHttpClient();
                client.networkInterceptors().add(new StethoInterceptor());
                mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new OkHttpStack(client));
            }
        }
        return mRequestQueue;
    }
}

Activity类代码:

public class MainActivity extends Activity {
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        tv = (TextView)findViewById(R.id.tv);

        RequestQueue queue = App.getInstance().getRequestQueue();
        String url = "https://publicobject.com/helloworld.txt";
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                LogUtil.d(s);
                tv.setText(s);
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                LogUtil.e(volleyError.toString());
            }
        });

        queue.add(request);

        SharedPrfUtil.setInt("uid",669);
        SharedPrfUtil.setString("username","dongye");
    }
}

实现效果如下图:

Stetho调试效果图
Stetho调试效果图

调试程序列表
调试程序列表

调试网络请求
调试网络请求

读取数据存储
读取数据存储