-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHttpUtils.java
More file actions
221 lines (209 loc) · 7.06 KB
/
HttpUtils.java
File metadata and controls
221 lines (209 loc) · 7.06 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.xzy.utils.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Objects;
/**
* Http请求的工具类。
*
* @author xzy
*/
@SuppressWarnings("unused")
public class HttpUtils {
private static final int TIMEOUT_IN_MILLIONS = 5000;
/**
* 回调接口。
*/
public interface HttpCallBackListener {
/**
* 回调成功。
* @param response 成功响应
*/
void onFinish(String response);
/**
* 回调失败。
* @param e 异常回调
*/
void onError(Exception e);
}
/**
* 异步的Get请求
*
* @param urlStr urlStr
* @param callBack CallBack
*/
public static void doGetAsync(final String urlStr,
final Map<String,String> params,
final HttpCallBackListener callBack) {
new Thread() {
public void run() {
try {
doGet(urlStr,params,callBack);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 异步的Post请求
*
* @param urlStr 请求 url
* @param params map 类型的请求参数
* @param callBack 回调
*/
public static void doPostAsync(final String urlStr,
final Map<String, String> params,
final HttpCallBackListener callBack) {
new Thread() {
public void run() {
try {
doPost(urlStr, params,callBack);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 供外部调用的 get 请求。
* @param path 请求 url
* @param params 请求参数 map 类型
*/
public static void doGet(String path, Map<String, String> params,
HttpCallBackListener httpCallBackListener){
StringBuilder sb = new StringBuilder(path);
if(params!=null && !params.isEmpty()){
sb.append("?");
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append("=");
try {
sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
if(httpCallBackListener != null){
httpCallBackListener.onError(e);
}
}
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
}
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(sb.toString()).openConnection();
} catch (IOException e) {
if(httpCallBackListener != null){
httpCallBackListener.onError(e);
}
}
Objects.requireNonNull(conn).setConnectTimeout(5000);
try {
conn.setRequestMethod("GET");
} catch (ProtocolException e) {
if(httpCallBackListener!= null){
httpCallBackListener.onError(e);
}
}
try {
if(conn.getResponseCode() == 200){
InputStream in = conn.getInputStream();
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
String line;
StringBuilder response = new StringBuilder();
while((line = bufferReader.readLine())!= null){
response.append(line);
}
if(httpCallBackListener != null){
httpCallBackListener.onFinish(response.toString());
}
}
} catch (IOException e) {
if(httpCallBackListener != null){
httpCallBackListener.onError(e);
}
}finally {
conn.disconnect();
}
}
/**
* @param path 请求的 url
* @param params map 类型的请求参数
* @param httpCallBackListener 回调
*/
public static void doPost(String path, Map<String, String> params,
HttpCallBackListener httpCallBackListener){
StringBuilder sb = new StringBuilder();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append("=");
try {
sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
if(httpCallBackListener != null){
httpCallBackListener.onError(e);
}
}
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
}
byte[] data = sb.toString().getBytes();
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(path).openConnection();
}catch (IOException e) {
if(httpCallBackListener != null){
httpCallBackListener.onError(e);
}
}
Objects.requireNonNull(conn).setConnectTimeout(5000);
try {
conn.setRequestMethod("POST");
} catch (ProtocolException e) {
if(httpCallBackListener != null){
httpCallBackListener.onError(e);
}
}
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length+"");
OutputStream outStream = null;
try {
outStream = conn.getOutputStream();
} catch (IOException e) {
if(httpCallBackListener != null){
httpCallBackListener.onError(e);
}
}
try {
Objects.requireNonNull(outStream).write(data);
outStream.flush();
if(conn.getResponseCode() == 200){
InputStream in = conn.getInputStream();
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
String line;
StringBuilder response = new StringBuilder();
while((line = bufferReader.readLine())!= null){
response.append(line);
}
if(httpCallBackListener != null){
httpCallBackListener.onFinish(response.toString());
}
}
} catch (IOException e) {
if(httpCallBackListener != null){
httpCallBackListener.onError(e);
}
}finally {
conn.disconnect();
}
}
}