-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncClientUtils
307 lines (299 loc) · 13.1 KB
/
AsyncClientUtils
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
package com.example.yueyundong1.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import android.content.Context;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.FileAsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
/**
* android-async-http工具类
* @author vagrant QQ:513302092
* @date 创建时间: 2015年4月25日
*/
public class AsyncClientUtils {
public static final String BASE_URL = "http://www.birdboy.cn/";
private static RequestParams requestParams;
private static AsyncHttpClient client = new AsyncHttpClient();
/**
* 使用android-async-http通过get方式向服务器发起请求,异步返回结果
* @param url 完整的URL路径
* @param params RequestParams封装的参数对
* @param responseHandler 回调接口
*/
public static void getWithFullUrl(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(context, url, params, responseHandler);
}
/**
* 使用android-async-http通过get方式向服务器发起请求,异步返回结果
* @param url 完整的URL路径
* @param name 参数名
* @param value 参数值
* @param responseHandler 回调接口
*/
public static void getWithFullUrl(Context context, String url, String name, String value, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams(name, value);
getWithFullUrl(context, url, requestParams, responseHandler);
}
/**
* 使用android-async-http通过get方式向服务器发起请求,异步返回结果
* @param url 完整的URL路径
* @param listParams List<BasicNameValuePair>格式参数
* @param responseHandler 回调接口
*/
public static void getWithFullUrl(Context context, String url, List<BasicNameValuePair> listParams, AsyncHttpResponseHandler responseHandler) {
Map<String, String> map = new LinkedHashMap<String, String>();
map = convertListUrlToMap(listParams);
getWithFullUrl(context, url, map, responseHandler);
}
/**
* 使用android-async-http通过get方式向服务器发起请求,异步返回结果
* @param url 完整的URL路径
* @param params Map<String, String>格式参数
* @param responseHandler 回调接口
*/
public static void getWithFullUrl(Context context, String url, Map<String, String> params, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams(params);
getWithFullUrl(context, url, requestParams, responseHandler);
}
/**
* 使用android-async-http通过get方式向服务器发起请求,异步返回结果
* @param api api名称
* @param params RequestParams封装的参数对
* @param responseHandler 回调接口
*/
public static void getWithApi(Context context, String api, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(context, getAbsoluteUrl(api), params, responseHandler);
}
/**
* 使用android-async-http通过get方式向服务器发起请求,异步返回结果
* @param api api名称
* @param name 参数名
* @param value 参数值
* @param responseHandler 回调接口
*/
public static void getWithApi(Context context, String api, String name, String value, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams(name, value);
getWithApi(context, api, requestParams, responseHandler);
}
/**
* 使用android-async-http通过get方式向服务器发起请求,异步返回结果
* @param api api名称
* @param listParams List<BasicNameValuePair>格式参数
* @param responseHandler 回调接口
*/
public static void getWithApi(Context context, String api, List<BasicNameValuePair> listParams, AsyncHttpResponseHandler responseHandler) {
Map<String, String> map = new LinkedHashMap<String, String>();
map = convertListUrlToMap(listParams);
getWithApi(context, api, map, responseHandler);
}
/**
* 使用android-async-http通过get方式向服务器发起请求,异步返回结果
* @param api api名称
* @param params Map<String, String>格式参数
* @param responseHandler 回调接口
*/
public static void getWithApi(Context context, String api, Map<String, String> params, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams(params);
getWithApi(context, api, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器发起请求,异步返回结果
* @param api 完整URL路径
* @param params RequestParams封装的参数
* @param responseHandler 回调接口
*/
public static void postWithFullUrl(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(context, url, params, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器发起请求,异步返回结果
* @param url 完整URL路径
* @param name name值
* @param value value值
* @param responseHandler 回调接口
*/
public static void postWithFullUrl(Context context, String url, String name, String value, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams(name, value);
postWithFullUrl(context, url, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器发起请求,异步返回结果
* @param url 完整URL路径
* @param listParams List<BasicNameValuePair>表示参数值
* @param responseHandler 回调接口
*/
public static void postWithFullUrl(Context context, String url, List<BasicNameValuePair> listParams, AsyncHttpResponseHandler responseHandler) {
Map<String, String> map = new LinkedHashMap<String, String>();
map = convertListUrlToMap(listParams);
postWithFullUrl(context, url, map, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器发起请求,异步返回结果
* @param url 完整URL路径
* @param params Map<String, String>表示参数值
* @param responseHandler 回调接口
*/
public static void postWithFullUrl(Context context, String url, Map<String, String> params, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams(params);
postWithFullUrl(context, url, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器发起请求,异步返回结果
* @param api api名称
* @param params RequestParams封装的参数
* @param responseHandler 回调接口
*/
public static void postWithApi(Context context, String api, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(context, getAbsoluteUrl(api), params, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器发起请求,异步返回结果
* @param api api名称
* @param name name值
* @param value value值
* @param responseHandler 回调接口
*/
public static void postWithApi(Context context, String api, String name, String value, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams(name, value);
postWithApi(context, api, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器发起请求,异步返回结果
* @param api api名称
* @param listParams List<BasicNameValuePair>形式表示参数
* @param responseHandler 回调接口
*/
public static void postWithApi(Context context, String api, List<BasicNameValuePair> listParams, AsyncHttpResponseHandler responseHandler) {
Map<String, String> map = new LinkedHashMap<String, String>();
map = convertListUrlToMap(listParams);
postWithApi(context, api, map, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器发起请求,异步返回结果
* @param api api名称
* @param params Map<String,String> 形式表示参数
* @param responseHandler 回调接口
*/
public static void postWithApi(Context context, String api, Map<String,String> params, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams(params);
postWithApi(context, api, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器写入InputStream,异步返回结果
* @param url url地址
* @param key 标识参数的key值 see: RequestParams.put(String key, InputStream stream)
* @param inputstream
* @param responseHandler 回调接口
*/
public static void postInputStream(Context context, String url, String key, InputStream inputstream, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams();
requestParams.put(key, inputstream);
postWithFullUrl(context, url, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器写入InputStream,异步返回结果
* @param url url地址
* @param key 标识参数的key值 see: RequestParams.put(String key, InputStream stream, String name)
* @param inputStream
* @param name inputStream的名称
* @param responseHandler 回调接口
*/
public static void postInputStream(Context context, String url, String key, InputStream inputStream, String name, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams();
requestParams.put(key, inputStream, name);
postWithFullUrl(context, url, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器写入InputStream,异步返回结果
* @param url url地址
* @param key 标识参数的key值 see: RequestParams.put(String key, InputStream stream, String name, String contentType)
* @param inputStream
* @param name inputStream的名称
* @param contentType inputStream的类型 如:application/json
* @param responseHandler 回调接口
*/
public static void postInputStream(Context context, String url, String key, InputStream inputStream, String name, String contentType, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams();
requestParams.put(key, inputStream, name, contentType);
postWithFullUrl(context, url, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器上传文件,异步返回结果
* @param url url地址
* @param key 标识参数的key值 see:RequestParams.put(String key, File file)
* @param file 文件
* @param responseHandler 回调接口
*/
public static void postFile(Context context, String url, String key, File file, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams();
try {
requestParams.put(key, file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
postWithFullUrl(context, url, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器上传文件,异步返回结果
* @param url url地址
* @param key 标识参数的key值 see:RequestParams.put(String key, File file, String contentType, String customFileName)
* @param file 文件
* @param contentType 文件类型 如:application/json
* @param responseHandler 回调接口
*/
public static void postFile(Context context, String url, String key, File file, String contentType, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams();
try {
requestParams.put(key, file, contentType);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
postWithFullUrl(context, url, requestParams, responseHandler);
}
/**
* 使用android-async-http通过post方式向服务器上传文件,异步返回结果
* @param url url地址
* @param key 标识参数的key值 see RequestParams.put(String key, File file, String contentType, String customFileName)
* @param file 文件
* @param contentType 文件类型 如:application/json
* @param customName 文件名
* @param responseHandler 回调接口
*/
public static void postFile(Context context, String url, String key, File file, String contentType, String customName, AsyncHttpResponseHandler responseHandler) {
requestParams = new RequestParams();
try {
requestParams.put(key, file, contentType, customName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
postWithFullUrl(context, url, requestParams, responseHandler);
}
public static void downloadFile(Context context, String url, FileAsyncHttpResponseHandler fileAsyncHttpResponseHandler) {
client.get(context, url, fileAsyncHttpResponseHandler);
}
public static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
/**
* 转换List<BasicNameValuePair>参数为Map<String, String>参数
* @param params List<BasicNameValuePair>形式参数
* @return Map<String, String>参数
*/
public static Map<String, String> convertListUrlToMap(List<BasicNameValuePair> params) {
String param = URLEncodedUtils.format(params, "utf-8");
Map<String, String> map = new LinkedHashMap<String, String>();
String[] pairs = param.split("&");
for (int i = 0 ; i < pairs.length; i++) {
String[] keyAndValue = pairs[i].split("=");
map.put(keyAndValue[0], keyAndValue[1]);
}
return map;
}
}