Title

使用Chrome来调试你的Android App

Github
使用Chrome来调试你的Android App
Facebook Stetho 使用学习
Android调试工具 stetho

####

添加依赖

1
2
3
4
5
6
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中启动配置

1
2
3
4
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]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//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 调试

参考列表
HTTP post body lost spuriously (HttpURLConnection & Jetty)
Post byte array in parameter
Convert a StringBuffer to a byte Array in Java

文章目录
  1. 1. 使用Chrome来调试你的Android App