优化:(在tese1,2中http请求代码基本相同 但我们每一次发送请求都编写一次代码)将通用的网络操作放到一个类HttpUil
HttpUil1
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
29public class HttpUtil {
public static void sendHttpRequest(final String address, final HttpCallbackListener listener) {
HttpURLConnection connection = null;
try {
URL url = new URL(address);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
若需发起请求(sendHttpRequest()内部没有开启线程 调用时可能阻塞主线程)1
2String address="http://wwww.baid.com";
String response=HttpUtil.sendHttpRequest(address);
解决方法(Java的回调机制)
首先定义接口 HttpCallbackListener1
2
3
4
5
6
7public interface HttpCallbackListener {
void onFinish(String response);
void onError(Exception e);
}
修改HttoUtil1
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
56public class HttpUtil {
public static void sendHttpRequest(final String address, final HttpCallbackListener listener) {
new Thread(new Runnable() {
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL(address);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
if (listener != null) {
// 回调onFinish()方法
listener.onFinish(response.toString());
}
} catch (Exception e) {
if (listener != null) {
// 回调onError()方法
listener.onError(e);
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
}
```
传入HttoCallbackListener实例
```java
HttpUtil.sendHttpRequest(address,new HttpCallbackListener()){
public void onFinish(String response){
//在这里根据返回内容执行具体逻辑
}
public void onError(Exception e){
//在这里对异常进行处理
}
}
使用HttpURIConnection太复杂 改用Okhttp
在HttpUtil内田间sendOkhttpRequest()方法1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public static void sendOkHttpRequest(final String address, final okhttp3.Callback callback) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(address)
.build();
client.newCall(request).enqueue(callback);
}
```
```java
HttpUtil.sendOkHttpRequest(address,new HttpCallbackListener()){
public void onFinish(String response){
//在这里根据返回内容执行具体逻辑
String responseData=response.body().strng();
}
public void onError(Exception e){
//在这里对异常进行处理
}
}