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/
如何集成
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可以看到:
![这里写图片描述]()
查看网络请求
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()
}
}
![这里写图片描述]()
总结: