/
HttpUtil.java
177 lines (161 loc) · 4.95 KB
/
HttpUtil.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
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
package bingo.android.util;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import bingo.android.exception.AppException;
public class HttpUtil {
public static final int HTTP_REQUEST_TIMEOUT_MS = 30 * 1000;
public static String get(String url) throws AppException {
String result = null;
try {
HttpClient httpClient = getHttpClient();
HttpGet httpGet = getHttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
int stateCode = httpResponse.getStatusLine().getStatusCode();
if (stateCode != HttpStatus.SC_OK)
throw AppException.http(stateCode);
HttpEntity entity = httpResponse.getEntity();
result = EntityUtils.toString(entity, HTTP.UTF_8);
} catch (ClientProtocolException e) {
e.printStackTrace();
throw AppException.http(e);
} catch (IOException e) {
e.printStackTrace();
throw AppException.network(e);
}
return result;
}
public static boolean post(String url) {
HttpPost httpPost = getHttpPOST(url);
try {
HttpResponse httpResponse = getHttpClient().execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static JSONObject getJSON(String url) throws AppException {
String result = HttpUtil.get(url);
// 如果没有返回有意义的内容或者错误信息,则视为运行错误(可能客户端传递参数有误,或者服务器端出错)
JSONObject json;
try {
json = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
throw AppException.data(e);
}
return json;
}
private static HttpPost getHttpPOST(String url) {
return new HttpPost(url);
}
private static HttpGet getHttpGet(String url) {
return new HttpGet(url);
}
private static HttpClient getHttpClient() {
HttpClient httpClient = new DefaultHttpClient();
final HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params,
HTTP_REQUEST_TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
ConnManagerParams.setTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
return httpClient;
}
// 以下为老版本
// public static String getRequest(String ulicrl) throws Exception {
// String result = null;
//
// HttpClient httpClient = new DefaultHttpClient();
//
// try {
// // 创建HttpGet对象。
// HttpGet get = new HttpGet(url);
// // 发送GET请求
// HttpResponse httpResponse = httpClient.execute(get);
// // 如果服务器成功地返回响应
// if (httpResponse.getStatusLine().getStatusCode() == 200) {
// // 获取服务器响应字符串
// HttpEntity entity = httpResponse.getEntity();
// Log.i("entity is null?", Boolean.toString(entity == null));
// result = EntityUtils.toString(entity, HTTP.UTF_8);
// Log.i("entity tostring is null?",
// Boolean.toString(result == null));
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// httpClient.getConnectionManager().shutdown();
// }
//
// return result;
// }
//
// /**
// *
// * @param url
// * 发送请求的URL
// * @param params
// * 请求参数
// * @return 服务器响应字符串
// * @throws Exception
// */
// public static String postRequest(String url, Map<String, String>
// rawParams) {
// String result = null;
// HttpClient httpClient = new DefaultHttpClient();
// try {
// // 创建HttpPost对象。
// HttpPost post = new HttpPost(url);
// // 如果传递参数个数比较多的话可以对传递的参数进行封装
// List<NameValuePair> params = new ArrayList<NameValuePair>();
// for (String key : rawParams.keySet()) {
// // 封装请求参数
// params.add(new BasicNameValuePair(key, rawParams.get(key)));
// }
// // 设置请求参数
// post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// // 发送POST请求
// HttpResponse httpResponse = httpClient.execute(post);
// // 如果服务器成功地返回响应
// if (httpResponse.getStatusLine().getStatusCode() == 200) {
// // 获取服务器响应字符串
// result = EntityUtils.toString(httpResponse.getEntity());
// return result;
// }
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// httpClient.getConnectionManager().shutdown();
// }
// return result;
// }
/**
* 获取json内容
*
* @param url
* @return JSONArray
* @throws JSONException
* @throws ConnectionException
*/
// public static String encode(String text) {
// return URLEncoder.encode(text);
// }
}