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除錯效果圖](https://i.iter01.com/images/24a4b560b1d241a9eb1b8c3640410f084a3abe8ef943c43bf9c2dd41258d3163.png)
![除錯程式列表](https://i.iter01.com/images/f9af4ed3b0e92a55e3d2302fef63fcd2235e59be90f6dcb4416d0d6fdc751a64.png)
![除錯網路請求](https://i.iter01.com/images/1cab3e78dd5274ff43cdb25d2df41d7752f3b252448032a308f439c500ec23f3.png)
![讀取資料儲存](https://i.iter01.com/images/9ae4060bd7fe7927fdc5eab583c89d1cd7d2329c3f33c586b20abd78d448efb7.png)