Google浏览器调试app -- Stetho(可调试网络,资源)

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();
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章