Stetho Study
一、Stetho概述
二、如何使我們的app的信息輸入到Chrome上
環境配置
首先添加依賴:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.facebook.stetho:stetho:1.0.1'
compile 'com.facebook.stetho:stetho-okhttp:1.0.1'
}
compile ‘com.facebook.stetho:stetho:1.0.1’
是添加的依賴包。
compile ‘com.facebook.stetho:stetho-okhttp:1.0.1’
本身只是繼承了一個okhttp的攔截器,同時該包還依賴okhttp-2.2.0。因此我們不需要再添加okhttp的依賴。
插入代碼
我們需要全局設置stetho的初始化類,使得Chrome可以監聽我們的app。主要分分兩步,第一步是app基本參數的監聽:
於是我們在自定義Application中onCreate中,添加如下代碼:
Stetho.initialize(
Stetho.newInitializerBuilder(this).enableDumpapp(
Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(
Stetho.defaultInspectorModulesProvider(this))
.build());
然後在Manifest裏聲明,OK!
但是這樣初始化無法監聽網絡訪問的請求。於是需要在網絡訪問模塊裏單獨設置,即第二步:
Stetho支持兩種方式的網絡訪問機制的監聽,一是okhttp,二是HttpUrlConnection。
如果你是用的是HttpUrlConnection,你需要使用StethoURLConnectionManager來進行集成。然後必須聲明Accept-Encoding: gzip的請求
headers。具體用法見facebook stetho源碼的sample。
最有效的,也是最簡單的方式是okhttp。
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());
原理是,okhttp本身就有攔截器機制,即攔截器攔截request,根據該請求的情況二次處理。而這裏我們向OkHttpClient對象添加新的攔截器。
client.networkInterceptors()的含義是返回此client具有的攔截器集合。
因此可以在使用網絡的地方,或者建立一個全局的client對象,對client進行處理。
使用Chrome
打開Chrome,輸入 chrome://inspect 然後就可以在列表裏看到有你的app可以用stetho進行調試的app。
此時手機有幾個運行添加stetho監聽的app,就會出現在Chrome中。然後點開,就會看到類似DDMS的調試器。它的好處在於可以脫離IDE進行測試,
還可以進行一些對持久化數據存儲的修改。
注意更改程序後,需要重新打開。
四、最後
爲了顯示和源碼Sample的不同,貼上使用okhttp訪問網絡使用Stetho進行調試的Demo。
MainActivity.java
package com.stethodemo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.facebook.stetho.okhttp.StethoInterceptor;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.img);
networkTest();
sqlTest();
}
/*********************************************************************************************/
private void sqlTest() {
new SqlTest(this, "db_con", null, 1);
}
private void networkTest() {
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayImage(imageView, "http://pic.cnr.cn/pic/shehui/20150902/W020150902291344708813.jpg");
}
});
}
/**
* 加載圖片
*
* @param view
* @param url
* @throws IOException
*/
private void displayImage(final ImageView view, final String url) {
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());
final Request request = new Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) {
InputStream is = null;
try {
is = response.body().byteStream();
try {
is.reset();
} catch (IOException e) {
e.printStackTrace();
}
final Bitmap bm = BitmapFactory.decodeStream(is, null, null);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
view.setImageBitmap(bm);
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
MyApplication.java
package com.stethodemo;
import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import com.facebook.stetho.Stetho;
/**
* Created by wom on 2015/9/1.
*/
public class MyApplication extends Application {
SharedPreferences myPreferences;
public void onCreate() {
super.onCreate();
Stetho.initialize(
Stetho.newInitializerBuilder(this).enableDumpapp(
Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(
Stetho.defaultInspectorModulesProvider(this))
.build());
sharedPreferencesTest();
}
private void sharedPreferencesTest() {
myPreferences = getSharedPreferences("SharedPreferencesTest", Activity.MODE_APPEND);
SharedPreferences.Editor editor = myPreferences.edit();
editor.putInt("llll", 1111);
editor.apply();
}
}