30
30
*/
31
31
public class DefaultImageHttpClient extends AbstractImageHttpClient {
32
32
33
- //private static final Logger LOG = LoggerFactory.getLogger(DefaultImageHttpClient.class);
34
-
35
33
private OkHttpClient mOkHttpClient = new OkHttpClient ();
36
34
37
35
public DefaultImageHttpClient (ClientConfig config ) {
38
36
super (config );
39
37
}
40
38
41
- // 获得异常发生时的返回信息
42
- /* private String getExceptionMsg(HttpRequest httpRequest, String exceptionStr) {
43
- String errMsg = new StringBuilder("HttpRequest:").append(httpRequest.toString())
44
- .append("\nException:").append(exceptionStr).toString();
45
- LOG.error(errMsg);
46
- return errMsg;
47
- }*/
48
-
49
39
@ Override
50
40
protected String sendGetRequest (HttpRequest httpRequest ) throws AbstractImageException {
51
41
mOkHttpClient .setProxy (config .getProxy ());
@@ -86,55 +76,10 @@ protected String sendGetRequest(HttpRequest httpRequest) throws AbstractImageExc
86
76
throw new UnknownException (e .getMessage ());
87
77
}
88
78
return string ;
89
-
90
- /*
91
- String url = httpRequest.getUrl();
92
- HttpGet httpGet = null;
93
- String responseStr = "";
94
- int retry = 0;
95
- int maxRetryCount = this.config.getMaxFailedRetry();
96
- while (retry < maxRetryCount) {
97
- try {
98
- URIBuilder urlBuilder = new URIBuilder(url);
99
- for (String paramKey : httpRequest.getParams().keySet()) {
100
- urlBuilder.addParameter(paramKey, String.valueOf(httpRequest.getParams().get(paramKey)));
101
- }
102
- httpGet = new HttpGet(urlBuilder.build());
103
- } catch (URISyntaxException e) {
104
- String errMsg = "Invalid url:" + url;
105
- LOG.error(errMsg);
106
- throw new ParamException(errMsg);
107
- }
108
-
109
- httpGet.setConfig(onGetConfig());
110
- setHeaders(httpGet, httpRequest.getHeaders());
111
-
112
- try {
113
- HttpResponse httpResponse = httpClient.execute(httpGet);
114
- responseStr = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
115
- new JSONObject(responseStr);
116
- return responseStr;
117
- } catch (ParseException | IOException e) {
118
- ++retry;
119
- if (retry == maxRetryCount) {
120
- String errMsg = getExceptionMsg(httpRequest, e.toString());
121
- throw new ServerException(errMsg);
122
- }
123
- } catch (JSONException e) {
124
- String errMsg = getExceptionMsg(httpRequest, e.toString());
125
- throw new ServerException(errMsg);
126
- } finally {
127
- httpGet.releaseConnection();
128
- }
129
- }
130
- return responseStr;
131
- */
132
-
133
79
}
134
80
135
81
@ Override
136
82
public void shutdown () {
137
- //mOkHttpClient.
138
83
}
139
84
140
85
@ Override
@@ -237,71 +182,13 @@ protected String sendPostRequest(HttpRequest httpRequest) throws AbstractImageEx
237
182
} else {
238
183
throw new ParamException ("Unknown ContentType, httpRequest.getContentType():" + httpRequest .getContentType ());
239
184
}
240
-
241
- /*
242
- String url = httpRequest.getUrl();
243
- String responseStr = "";
244
- int retry = 0;
245
- int maxRetryCount = this.config.getMaxFailedRetry();
246
- while (retry < maxRetryCount) {
247
- HttpPost httpPost = new HttpPost(url);
248
- httpPost.setConfig(onGetConfig());
249
-
250
- Map<String, Object> params = httpRequest.getParams();
251
- setHeaders(httpPost, httpRequest.getHeaders());
252
- if (httpRequest.getContentType() == HttpContentType.APPLICATION_JSON) {
253
- setJsonEntity(httpPost, params);
254
- } else if (httpRequest.getContentType() == HttpContentType.MULTIPART_FORM_DATA) {
255
- try {
256
- HashMap<String, String> keyList = httpRequest.getKeyList();
257
- HashMap<String, File> imageList = httpRequest.getImageList();
258
- String imageKey = httpRequest.getImageKey();
259
- File image = httpRequest.getImage();
260
- setMultiPartEntity(httpPost, params, imageList, image, keyList, imageKey);
261
- } catch (Exception e) {
262
- throw new UnknownException(e.toString());
263
- }
264
- }
265
-
266
- try {
267
- HttpResponse httpResponse = httpClient.execute(httpPost);
268
- responseStr = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
269
- new JSONObject(responseStr);
270
- return responseStr;
271
- } catch (ParseException | IOException e) {
272
- ++retry;
273
- if (retry == maxRetryCount) {
274
- String errMsg = getExceptionMsg(httpRequest, e.toString());
275
- throw new ServerException(errMsg);
276
- }
277
- } catch (JSONException e) {
278
- String errMsg = getExceptionMsg(httpRequest, e.toString());
279
- throw new ServerException(errMsg);
280
- } finally {
281
- httpPost.releaseConnection();
282
- }
283
- }
284
- return responseStr;
285
- */
286
185
}
287
186
288
- /*
289
- private void setJsonEntity(HttpPost httpPost, Map<String, Object> params) {
290
- ContentType utf8TextPlain = ContentType.create("text/plain", Consts.UTF_8);
291
- JSONObject root = new JSONObject(params);
292
- StringEntity stringEntity = new StringEntity(root.toString(), utf8TextPlain);
293
- httpPost.setEntity(stringEntity);
294
- }
295
- */
296
-
297
- private void setMultiPartEntity (MultipartBuilder multipartBuilder ,/*HttpPost httpPost,*/ Map <String , Object > params , Map <String , File > images , File imageData , Map <String , String > keyList , String imageKey )
187
+ private void setMultiPartEntity (MultipartBuilder multipartBuilder , Map <String , Object > params , Map <String , File > images , Map <String , byte []> contentList ,File imageData , Map <String , String > keyList , String imageKey )
298
188
throws FileNotFoundException {
299
- //ContentType utf8TextPlain = ContentType.create("text/plain", Consts.UTF_8);
300
- //MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
301
189
302
190
multipartBuilder .type (MultipartBuilder .FORM );
303
191
for (String paramKey : params .keySet ()) {
304
- //entityBuilder.addTextBody(paramKey, String.valueOf(params.get(paramKey)), utf8TextPlain);
305
192
multipartBuilder .addFormDataPart (paramKey , String .valueOf (params .get (paramKey )));
306
193
}
307
194
@@ -315,15 +202,13 @@ private void setMultiPartEntity(MultipartBuilder multipartBuilder,/*HttpPost htt
315
202
if (!imageFile .exists ()) {
316
203
throw new FileNotFoundException ("File Not Exists: " + imageFile .getAbsolutePath ());
317
204
}
318
- //entityBuilder.addBinaryBody(paramName, imageFile);
319
205
multipartBuilder .addPart (
320
206
Headers .of ("Content-Disposition" , String .format ("form-data; name=\" %s\" " , paramName )),
321
207
RequestBody .create (MediaType .parse ("image/jpg" ), imageFile )
322
208
);
323
209
}
324
210
} else if (imageData != null ) {
325
211
if (imageData .exists ()) {
326
- //entityBuilder.addBinaryBody(imageKey, imageData);
327
212
multipartBuilder .addPart (
328
213
Headers .of ("Content-Disposition" , String .format ("form-data; name=\" %s\" " , imageKey )),
329
214
RequestBody .create (MediaType .parse ("image/jpg" ), imageData )
@@ -343,27 +228,5 @@ private void setMultiPartEntity(MultipartBuilder multipartBuilder,/*HttpPost htt
343
228
}
344
229
}
345
230
346
- //HttpEntity entity = entityBuilder.build();
347
- //httpPost.setEntity(entity);
348
- }
349
-
350
- /**
351
- * 设置Http头部,同时添加上公共的类型,长连接,Image SDK标识
352
- * @param message HTTP消息
353
- * @param headers 用户额外添加的HTTP头部
354
- */
355
- /*
356
- private void setHeaders(HttpMessage message, Map<String, String> headers) {
357
- message.setHeader(RequestHeaderKey.ACCEPT, RequestHeaderValue.Accept.ALL);
358
- message.setHeader(RequestHeaderKey.CONNECTION, RequestHeaderValue.Connection.KEEP_ALIVE);
359
- message.setHeader(RequestHeaderKey.USER_AGENT, this.config.getUserAgent());
360
-
361
- if (headers != null) {
362
- for (String headerKey : headers.keySet()) {
363
- message.setHeader(headerKey, headers.get(headerKey));
364
- }
365
- }
366
231
}
367
- */
368
-
369
232
}
0 commit comments