https://github.com/facebook/stetho
http://facebook.github.io/stetho/
android神器Stetho调试
By dongye
2015-05-19
更新日期:2016-05-06

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

// Gradle dependency on Stetho  dependencies {  compile 'com.facebook.stetho:stetho:1.1.1'  }

2.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的可以点击这里)
1.添加依赖

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'

2.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() { @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调试效果图
调试程序列表 调试程序列表
调试网络请求 调试网络请求
读取数据存储 读取数据存储

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. android一句话实现APP自动更新(带通知栏)
  6. android应用程序中使用root权限
  7. Android(安卓)Studio:为Android定制的IDE
  8. android字体
  9. Android下使用Logcat打印信息

随机推荐

  1. 使用ComponentName启动Activity
  2. android 取设备公网ip ,内网ip
  3. Fragment里面嵌套ViewPager
  4. android辅助圆按钮
  5. Android第二次作业
  6. android studio gradle常用设置
  7. ==(Android)简易计算器==
  8. Android(安卓)-- Notification
  9. unity android获取电量和wifi信号强度
  10. android异步下载图片