-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathHttpManager.java
More file actions
358 lines (292 loc) · 10.2 KB
/
HttpManager.java
File metadata and controls
358 lines (292 loc) · 10.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.demo.manager;
import static uiauto.HttpManager.getResponse;
import static uiauto.HttpManager.getResponseBody;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.json.JSONException;
import uiauto.InputUtil;
import uiauto.UIAutoApp;
import zuo.biao.apijson.JSONRequest;
import zuo.biao.apijson.StringUtil;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.util.Log;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import apijson.demo.application.DemoApplication;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
/**HTTP请求管理类
* @author Lemon
* @use HttpManager.getInstance().get(...)或HttpManager.getInstance().post(...) > 在回调方法onHttpRequestSuccess和onHttpRequestError处理HTTP请求结果
* @must 解决getToken,getResponseCode,getResponseData中的TODO
*/
public class HttpManager {
private static final String TAG = "HttpManager";
private Context context;
private static HttpManager instance;// 单例
private HttpManager(Context context) {
this.context = context;
}
public static HttpManager getInstance() {
if (instance == null) {
synchronized (HttpManager.class) {
if (instance == null) {
instance = new HttpManager(DemoApplication.getInstance());
}
}
}
return instance;
}
/**
* 列表首页页码。有些服务器设置为1,即列表页码从1开始
*/
public static final int PAGE_NUM_0 = 0;
public static final String KEY_TOKEN = "token";
public static final String KEY_COOKIE = "cookie";
public static final MediaType TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
/**POST请求
* @param url_ 接口url
* @param request 请求
* @param requestCode
* 请求码,类似onActivityResult中请求码,当同一activity中以实现接口方式发起多个网络请求时,请求结束后都会回调
* {@link OnHttpResponseListener#onHttpResponse(int, String, Exception)}<br/>
* 在发起请求的类中可以用requestCode来区分各个请求
* @param listener
*/
public void post(final String url_, final com.alibaba.fastjson.JSONObject request
, final int requestCode, final OnHttpResponseListener listener) {
String tag = request == null ? null : request.getString(JSONRequest.KEY_TAG);
new AsyncTask<Void, Void, Exception>() {
String httpRequestString;
int responseCode;
String responseBody;
Headers responseHeaders;
@Override
protected Exception doInBackground(Void... params) {
try {
String url = UIAutoApp.getInstance().getHttpUrl(url_);
String token = getToken(url);
OkHttpClient client = getHttpClient(url);
if (client == null) {
return new Exception(TAG + ".post AsyncTask.doInBackground client == null >> return;");
}
String reqStr = JSON.toJSONString(request);
RequestBody requestBody = RequestBody.create(TYPE_JSON, reqStr);
Request httpRequest = new Request.Builder()
.addHeader(KEY_TOKEN, token).url(url)
.post(requestBody).build();
httpRequestString = reqStr;
UIAutoApp.getInstance().post(new Runnable() {
@Override
public void run() {
UIAutoApp.getInstance().onHTTPEvent(
InputUtil.HTTP_ACTION_POST, "JSON"
, "POST", "http://apijson.cn:8080", url_ + (tag == null ? "" : "/" + tag)
, httpRequest == null ? null : httpRequest.headers().toString(), reqStr, null
, getActivity(listener), getFragment(listener)
);
}
});
Response httpResponse = getResponse(client, httpRequest);
responseBody = getResponseBody(httpResponse);
responseCode = httpResponse.code();
responseHeaders = httpResponse.headers();
} catch (Exception e) {
android.util.Log.e(TAG, "post AsyncTask.doInBackground try { result = getResponseJson(..." +
"} catch (Exception e) {\n" + e.getMessage());
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception e) {
super.onPostExecute(e);
listener.onHttpResponse(requestCode, responseBody, e);
UIAutoApp.getInstance().post(new Runnable() {
@Override
public void run() {
UIAutoApp.getInstance().onHTTPEvent(
- InputUtil.HTTP_ACTION_POST, e == null ? ("" + responseCode) : e.getClass().getSimpleName()
, "POST", "http://apijson.cn:8080", url_ + (tag == null ? "" : "/" + tag)
, responseHeaders == null ? null : responseHeaders.toString(), httpRequestString, responseBody
, getActivity(listener), getFragment(listener)
);
}
});
}
}.execute();
}
private String toHttpJSONString(String headers, String body) {
return headers + "Content: \n" + body;
}
private Activity getActivity(OnHttpResponseListener listener) {
if (listener instanceof Activity) {
return (Activity)listener;
}
if (listener instanceof Fragment) {
Fragment fragment = (Fragment) listener;
return fragment.getActivity();
}
return null;
}
private Fragment getFragment(OnHttpResponseListener listener) {
return listener instanceof Fragment ? (Fragment)listener : null;
}
//httpGet/httpPost 内调用方法 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**
* @param url
* @return
*/
private OkHttpClient getHttpClient(String url) {
Log.i(TAG, "getHttpClient url = " + url);
if (StringUtil.isNotEmpty(url, true) == false) {
Log.e(TAG, "getHttpClient StringUtil.isNotEmpty(url, true) == false >> return null;");
return null;
}
OkHttpClient client = new OkHttpClient();
client.setCookieHandler(new HttpHead());
client.setConnectTimeout(15, TimeUnit.SECONDS);
client.setWriteTimeout(10, TimeUnit.SECONDS);
client.setReadTimeout(10, TimeUnit.SECONDS);
return client;
}
/**
* @param tag
* @must demo_***改为服务器设定值
* @return
*/
public String getToken(String tag) {
return context.getSharedPreferences(KEY_TOKEN, Context.MODE_PRIVATE).getString(KEY_TOKEN + tag, "");
}
/**
* @param tag
* @param value
*/
public void saveToken(String tag, String value) {
context.getSharedPreferences(KEY_TOKEN, Context.MODE_PRIVATE)
.edit()
.remove(KEY_TOKEN + tag)
.putString(KEY_TOKEN + tag, value)
.commit();
}
/**
* @return
*/
public String getCookie() {
return context.getSharedPreferences(KEY_COOKIE, Context.MODE_PRIVATE).getString(KEY_COOKIE, "");
}
/**
* @param value
*/
public void saveCookie(String value) {
context.getSharedPreferences(KEY_COOKIE, Context.MODE_PRIVATE)
.edit()
.remove(KEY_COOKIE)
.putString(KEY_COOKIE, value)
.commit();
}
/**
* @param client
* @param request
* @return
* @throws Exception
*/
private String getResponseJson(OkHttpClient client, Request request) throws Exception {
if (client == null || request == null) {
Log.e(TAG, "getResponseJson client == null || request == null >> return null;");
return null;
}
Response response = client.newCall(request).execute();
return response.isSuccessful() ? response.body().string() : null;
}
/**从object中获取key对应的值
* *获取如果T是基本类型容易崩溃,所以需要try-catch
* @param json
* @param key
* @return
* @throws JSONException
*/
public <T> T getValue(String json, String key) throws JSONException {
return getValue(JSON.parseObject(json), key);
}
/**从object中获取key对应的值
* *获取如果T是基本类型容易崩溃,所以需要try-catch
* @param object
* @param key
* @return
* @throws JSONException
*/
@SuppressWarnings("unchecked")
public <T> T getValue(JSONObject object, String key) throws JSONException {
return (T) object.get(key);
}
//httpGet/httpPost 内调用方法 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/**http请求头
*/
public class HttpHead extends CookieHandler {
public HttpHead() {
}
@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
String cookie = getCookie();
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.putAll(requestHeaders);
if (!TextUtils.isEmpty(cookie)) {
List<String> cList = new ArrayList<String>();
cList.add(cookie);
map.put("Cookie", cList);
List<String> idList = new ArrayList<String>();
idList.add(UIAutoApp.getInstance().getDelegateId());
map.put("Apijson-Delegate-Id", idList);
}
return map;
}
@Override
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {
List<String> list = responseHeaders.get("Set-Cookie");
if (list != null) {
for (int i = 0; i < list.size(); i++) {
String cookie = list.get(i);
if (cookie.startsWith("JSESSIONID")) {
saveCookie(cookie);
break;
}
}
}
List<String> idList = responseHeaders.get("Apijson-Delegate-Id");
if (idList != null && idList.isEmpty() == false) {
String id = idList.get(0);
int start = id.indexOf("JSESSIONID=");
int end = id.indexOf(";");
id = id.substring((start < 0 ? 0 : start) + "JSESSIONID=".length(), end < 0 ? id.length() : end);
UIAutoApp.getInstance().setDelegateId(StringUtil.getTrimedString(id));
}
}
}
}