AndroidStetho

Summary: Author: 张亚飞 | 阅读时间: 2 minute read | Published: 2016-03-26
Filed under Categories: LinuxTags: Note,

使用Chrome来调试你的Android App

添加依赖

compile 'com.facebook.stetho:stetho:1.2.0'

//调试 HttpURLConnection 建立的网络请求
compile 'com.facebook.stetho:stetho-urlconnection:1.2.0'
//调试 OKHttp 建立的网络请求
compile 'com.facebook.stetho:stetho-okhttp:1.2.0'

Application 中启动配置

Stetho.initialize(Stetho.newInitializerBuilder(this)
    .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
    .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
    .build());

使用 HttpUrlConnection 调试网络请求,导入官方的一个网络请求类 Networker [stetho/stetho-sample/src/main/java/com/facebook/stetho/sample/Networker.java]

//Post请求
public String sendHUCTPost(Context context, String url, Map<String, String> params) {
    PrintWriter out = null;
    BufferedReader in = null;
    String response = "";
    try {
        URL realUrl = new URL(url);
        // 打开和URL之间的连接
        //HttpURLConnection

        StringBuffer requestParams = new StringBuffer();
        if (params != null && params.size() > 0) {

            //conn.setDoOutput(true); // true indicates POST request

            // creates the params string, encode them using URLEncoder
            Iterator<String> paramIterator = params.keySet().iterator();
            while (paramIterator.hasNext()) {
                String key = paramIterator.next();
                String value = params.get(key);
                requestParams.append(URLEncoder.encode(key, "UTF-8"));
                //判断一下,防止传空值 null 程序[URLEncoder.encode(value, "UTF-8"]意外崩溃,不能用 value.isEmpty() 判断是否已赋值
                if (value != null)
                    requestParams.append("=").append(URLEncoder.encode(value, "UTF-8"));
                requestParams.append("&");
            }

            // sends POST data
//                OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
//                writer.write(requestParams.toString());
//                writer.flush();
            // 获取URLConnection对象对应的输出流
//                out = new PrintWriter(conn.getOutputStream());
//                // 发送请求参数
//                out.print(requestParams);
//                // flush输出流的缓冲
//                out.flush();
        }
        //byte[] postBytes = requestXML.getBytes("UTF-8");
        //byte[] postBytes = new byte[]{ (byte) 0x65, (byte)0x10, (byte)0xf3, (byte)0x29};
        // 将 StringBuffer 转换为 byte[] 数组 - 参考 http://stackoverflow.com/questions/8005327/convert-a-stringbuffer-to-a-byte-array-in-java
        byte[] postBytes = String.valueOf(requestParams).getBytes();
        Networker.HttpRequest request = Networker.HttpRequest.newBuilder()
                .friendlyName("ShangYun")
                .method(Networker.HttpMethod.POST)
                .url(url)
                .body(postBytes)
                .build();
        Networker.get().submit(request, mStoreRssResponse);

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return response;
}

//Get请求
public void sendOneReq() {
    Networker.HttpRequest request = Networker.HttpRequest.newBuilder()
            .friendlyName("Bai Du")
            .method(Networker.HttpMethod.GET)
            .url("http://www.baidu.com")
            .build();
    Networker.get().submit(request, mStoreRssResponse);
}

private final Networker.Callback mStoreRssResponse = new Networker.Callback() {
    @Override
    public void onResponse(Networker.HttpResponse result) {
        if (result.statusCode == 200) {
            String page22222=new String(result.body);
            System.out.println("page22222 : "+page22222);
        }else{
            System.out.println("result.statusCode is not 200");
        }
    }
    @Override
    public void onFailure(IOException e) {
        System.out.println("onFailure");
    }
};

最后在 Chrome 浏览器输入 chrome://inspect/#devices 调试

Reference

Comments

Cor-Ethan, the beverage → www.iirii.com