Android 借助Stetho在Chrome上調試Android網絡、數據庫、Sharedpreferences
轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/61919840
本文出自【趙彥軍的博客】
簡介
Stetho是Facebook開源的Andorid調試工具。當你的應用集成Stetho時,開發者可以訪問Chrome,在Chrome Developer Tools中查看應用布局,網絡請求,sqlite,preference等等,可視化一切應用操作(更重要的是不用root)。
官網: http://facebook.github.io/stetho/
如何集成
- 在
build.gradle添加
dependencies {
compile 'com.facebook.stetho:stetho-okhttp3:1.4.2'
}
- 初始化
package com.zyj.stetho;
import android.app.Application;
import com.facebook.stetho.Stetho;
/**
* Created by ${zhaoyanjun} on 2017/3/13.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
}
- 用數據線把手機和電腦連起來,運行App , 打開
Chrome輸入chrome://inspect/#devices。可以看到下面的界面。

chrome調試Android 數據庫、SharedPreferences
點擊inspect將會看到如下Developer Tools界面,如果這個界面出不來,看看是否需要翻牆,你懂得:

點擊數據庫的表,可以看到數據庫里面的數據內容:

點擊SharedPreferences可以看到:

查看網絡請求
- 首選基於
OkHttp3.x添加攔截器
void net(){
String url = "https://www.baidu.com/" ;
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor( new StethoInterceptor()) //添加攔截器
.build() ;
Request request = new Request.Builder()
.url(url)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
if ( response.isSuccessful() ) {
String result = response.body().string() ;
Log.e( "zhao", "net: " + result );
}
} catch (IOException e) {
e.printStackTrace();
}
}

總結:
-
本例子中的數據庫用的是:
lite-orm-1.9.2,jar
地址是:https://github.com/litesuits/android-lite-orm
博客地址:http://www.cnblogs.com/zhaoyanjun/p/5640788.html -
本例字中使用的網絡請求框架為:
okhttp
地址:https://github.com/square/okhttp -
這個文章所有的代碼以上傳至Github:https://github.com/zyj1609wz/Stetho
