okhttp边学边记

        学习安卓一定离不开网络访问,但是官方已经不推荐使用HttpClient了,我们需要一个好用的网络库,这时,就想到了okhttp。

一、导包

  • 对于Android Studio的用户:compile 'com.squareup.okhttp3:okhttp:(insert latest version)'(目前的最新版本是3.4.1)
  • 对于Eclipse的用户:可以直接下载[the lastest JAR],导入项目中即可。
  • 注意:官方文档里说到,“当使用到快速I/O和resizable buffers的时候需要okio”所以,还有一步:
    • 对于Android Studio的用户:compile 'com.squareup.okio:okio:(insert latest version)'(目前的最新版本是1.10.0)
    • 对于Eclipse的用户:下载最近的 [ the lastest JAR ] 即可

二、使用步骤:

  1. HttpGet
- 首先从最常用到的http get请求开始,例子:
- ```java
    //创建okHttp对象
    OkHttpClient client = new OkHttpClient();
    //创建一个Request
    final Request request = new Request.Builder().url("http://luciferlv.tech").build();
    //new call
    Call call = client.newCall(request);
    //请求加入调度
    call.enqueue(new CallBack(){
            @Override
            public void onFailure(Request request, IOException e){

            }

            @Override
            public void onResponse(final Response response) throws IOException{
                //String htmlStr = response.body().string();
            }
        })
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 代码分析:
1. 发送一个get请求的步骤:首先构造一个Request对象,参数起码有一个url,也可以通过Request.Builder来设置更多的参数如:`header和method`等。
2. 然后通过request的对象去构造得到一个Call对象,类似于将你的请求封装成了任务,既然是任务,就会有`execute()`和`cancel()`等方法。
3. 最后,我们希望以**异步**的方式去执行请求,所以我们调用的是`call.enqueue`,将call加入调度队列,然后等待任务执行完成,我们在CallBack中即可得到结果。
- **注意:**
- `onResponse`回调的参数是response,一般情况下,比如我们希望获得返回的字符串,可以通过`response.body().string()`获取;如果希望获得返回的二进制字节数组,则调用`response.body().bytes();`如果想拿到返回的inputStream,则调用`response.body().byteStream();`,我们能拿到`inputStream`就可以通过IO的方式下载大文件(写文件)。
- 还有一个问题`onResponse`执行的线程并不是UI线程。如果希望操作控件,还是需要用到handler等。如:
- ```java
@Override
public void onResponse(final Response response) throws IOException{
final String res = response.body().string();
runOnUiThread(new Runnable(){
@Override
public void run(){
mTextView.setText(res);
}
});
}
- 我们这里是异步的方式去执行,当然也支持阻塞的方式,上面也说了Call有一个`execute()`方法,你也可以直接调用`call.execute()`通过返回一个`Response`。
  1. HttpPost携带参数
- 当请求需要提交参数的时候,我们就会用到post请求,和Get不同的就是Request构造的不同
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, null);
FormEncodingBuilder builder = new FormEncodingBuilder();
builder.add("username", "Lucifer");

Request request = new Request.Builder.url(url).post(builder.build()).build();
client.newCall(request).enqueue(new CallBack(){
@Override
public void onFailure(Request request, IOException e){

}

@Override
public void onResponse(final Response response) throws IOException{
//
}
});
- 大家都清除,post的矢耦,参数是包含在请求体中的;所以我们通过FormEncodingBuilder。添加多个String键值对,然后去构造RequestBody,最后完成我们Request的构造。后面的就和上面的是一样的了。
  1. 基于http的文件上传
- 接下来我们再介绍一个可以构造RequestBody的Builder,叫做`MultipartBuilder`。当我们需要做类似与表单上传的时候,就可以用它来构造我们的requestBody。
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
File file = new File(Environment.getExternalStorageDirectory(), "balabala.mp4");

RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);

RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addPart(Headers.of("Content-Dispostion", "form-data; name=\"username\""), RequestBody.create(null, "Lucifer"))
.addPart(Heafers.of("Content-Dispostion", "form-data; name=\"mFile\"; fileName=\"wjd.mp4\""), fileBody)
.build();

Request request = new Request.Builder()
.url("http://192.168.1.103:8080/okHttpServer/fileUpload")
.post(requestBody)
.build();

Call call = client.newCall(request);
call.enqueue(new CallBack(){
@Override
public void onFailure(Request request, IOException e){

}

@Override
public void onResponse(final Response response) throws IOException{
//
}
});
- 上述代码向服务器传递了一个键值对`username: Lucifer`和一个文件。我们通过MultipartBuilder的addPart()方法可以添加键值对或者文件。 - 其实类似于我们拼接浏览器行为的方式 - **图片下载:**通过回调的Response拿到byte[]然后decode成图片; - **文件下载:**通过拿到inputStream做写文件操作。

三、封装

  • 由于按照上述的代码,请求多了重复的代码就多了,我们就希望封装这些代码
  1. 使用:
  2. 一般的get请求
1
2
3
4
5
6
7
8
9
10
11
OkHttpClientManager.getAsyn("https://www.baidu.com", new OkHttpClientManager.ResultCallBack<String>(){
@Override
public void onError(Request request, Exception e){
e.printStackTrace();
}

@Override
public void onResponse(String u){
mTextView.setText(u);//这里是UI线程
}
});
- 对于一把的请求,我们希望给个url,然后CallBack里面直接操作控件。 2. 文件上传且携带参数 - 我们希望提供一个方法,传入url, params, file, callback即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
OkHttpClientManager.postAsyn("http://192.168.1.103:8080/okHttpServer/fileUpload", //参数1
new OkHttpClientManager.ResultCallBack<String>(){
@Override
public void onError(Request request, IOException e){
e.printStackTrace();
}

@Override
public void onResponse(String result){

}
},//参数2
file, //参数3
"mFile",//参数4
new OkHttpClientManager.Param[]{
new OkHttpClientManager.param("username", "lt"),
new OkHttpClientManager.param("password", "123")
});
- 参数3为file,参数4为file对应的name,这个name不是文件的名字; - 对应于http中的`<input type="file" name="mFile" >` - 对应的是name后面的值,即mFile。 3. 文件下载 - 对于文件下载,提供url,目标dir,callback即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
OkHttpClientManager.downloadAsyn("http://192.168.1.103:8080/okHttpServer/files/message_01.png",//
Environment.getExternalStorageDirectory().getAbsolutePath(),//
new OkHttpClientManager.ResultCallBack<String>(){
@Override
public void onError(Request request, IOException e){

}

@Override
public void onResponse(String response){
//文件下载成功,这里回调的response为文件的absolutePath
}
});
4. 展示图片 - 展示图片,我们希望提供一个url和一个imageView,如果下载成功,直接帮我们设置上即可。
1
OkHttpClientManager.displayImage(mImageView, "http://192.168.1.103:8080/20150817/1.jpg");
- 内部会自动根据imageView的大小自动对图片进行合适的压缩。虽然,这里可能不适合一次性加载大量图片的场景,但是对于app中偶尔有几个图片的加载,还是可用的。

四、整合Gson

  • 项目中使用时,服务端返回的是Json字符串,希望客户端回调可以直接拿到对象,于是整合进入Gson。
  1. 直接回调对象
- 例如现在有个User实体类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class User{
public String username;
public String password;

public User(){

}

public User(String username, String password){
this.username = username;
this.password = password;
}

@Override
public String toString(){
return "User{"+
"username='" + username + '\'' +
",password='" + password + '\'' +
"}";
}
}
- 服务端返回:``{"username":"lt", "password":"123"}`` - 客户端可以如下方式的调用:
1
2
3
4
5
6
7
8
9
10
11
12
OkHttpClientManager.getAsyn("http://192.168.56.1:8080/okHttpServer/user!getUser",
new OkHttpClientManager.ResultCallBack<User>(){
@Override
public void onError(Request request, Exception e){
e.printStackTrace();
}

@Override
public void onResponse(User user){
mTextView.setText(user.toString());//UI线程
}
});
- 我们传入泛型User,在onResponse里面直接回调User对象。 - **注意:**如果在`json字符串->实体对象`过程中发生错误,程序不会崩溃,`onError`方法会被回调。
  1. 回调对象集合
- 依然是上述的User类,服务端返回
  - ``[{"username":"lt","password":"123"},{"username":"chl","password":"12345"}]``
- 则客户端可以如下调用:
  - ```java
      OkHttpClientManager.getAsyn("http://192.168.56.1:8080/okHttpServer/user!getUsers",//
          new OkHttpClientManager.ResultCallBack<List<User>>(){
              @Override
              public void onError(Request request, Exception e){
                  e.printStackTrace();
              }

              @Override
              public void onResponse(List<User> us){
                  Log.e("TAG", us.size() + " ");
                  mTextView.setText(us.get(1).toString());
              }
          });
    
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
      - 唯一的区别,就是泛型变成了`List<User>`。


### OkHttpClientManager的源码
```java
public class OkHttpClientManager
{
private static OkHttpClientManager mInstance;
private OkHttpClient mOkHttpClient;
private Handler mDelivery;
private Gson mGson;


private static final String TAG = "OkHttpClientManager";

private OkHttpClientManager()
{
mOkHttpClient = new OkHttpClient();
//cookie enabled
mOkHttpClient.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
mDelivery = new Handler(Looper.getMainLooper());
mGson = new Gson();
}

public static OkHttpClientManager getInstance()
{
if (mInstance == null)
{
synchronized (OkHttpClientManager.class)
{
if (mInstance == null)
{
mInstance = new OkHttpClientManager();
}
}
}
return mInstance;
}

/**
* 同步的Get请求
*
* @param url
* @return Response
*/
private Response _getAsyn(String url) throws IOException
{
final Request request = new Request.Builder()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
Response execute = call.execute();
return execute;
}

/**
* 同步的Get请求
*
* @param url
* @return 字符串
*/
private String _getAsString(String url) throws IOException
{
Response execute = _getAsyn(url);
return execute.body().string();
}


/**
* 异步的get请求
*
* @param url
* @param callback
*/
private void _getAsyn(String url, final ResultCallback callback)
{
final Request request = new Request.Builder()
.url(url)
.build();
deliveryResult(callback, request);
}


/**
* 同步的Post请求
*
* @param url
* @param params post的参数
* @return
*/
private Response _post(String url, Param... params) throws IOException
{
Request request = buildPostRequest(url, params);
Response response = mOkHttpClient.newCall(request).execute();
return response;
}


/**
* 同步的Post请求
*
* @param url
* @param params post的参数
* @return 字符串
*/
private String _postAsString(String url, Param... params) throws IOException
{
Response response = _post(url, params);
return response.body().string();
}

/**
* 异步的post请求
*
* @param url
* @param callback
* @param params
*/
private void _postAsyn(String url, final ResultCallback callback, Param... params)
{
Request request = buildPostRequest(url, params);
deliveryResult(callback, request);
}

/**
* 异步的post请求
*
* @param url
* @param callback
* @param params
*/
private void _postAsyn(String url, final ResultCallback callback, Map<String, String> params)
{
Param[] paramsArr = map2Params(params);
Request request = buildPostRequest(url, paramsArr);
deliveryResult(callback, request);
}

/**
* 同步基于post的文件上传
*
* @param params
* @return
*/
private Response _post(String url, File[] files, String[] fileKeys, Param... params) throws IOException
{
Request request = buildMultipartFormRequest(url, files, fileKeys, params);
return mOkHttpClient.newCall(request).execute();
}

private Response _post(String url, File file, String fileKey) throws IOException
{
Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, null);
return mOkHttpClient.newCall(request).execute();
}

private Response _post(String url, File file, String fileKey, Param... params) throws IOException
{
Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, params);
return mOkHttpClient.newCall(request).execute();
}

/**
* 异步基于post的文件上传
*
* @param url
* @param callback
* @param files
* @param fileKeys
* @throws IOException
*/
private void _postAsyn(String url, ResultCallback callback, File[] files, String[] fileKeys, Param... params) throws IOException
{
Request request = buildMultipartFormRequest(url, files, fileKeys, params);
deliveryResult(callback, request);
}

/**
* 异步基于post的文件上传,单文件不带参数上传
*
* @param url
* @param callback
* @param file
* @param fileKey
* @throws IOException
*/
private void _postAsyn(String url, ResultCallback callback, File file, String fileKey) throws IOException
{
Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, null);
deliveryResult(callback, request);
}

/**
* 异步基于post的文件上传,单文件且携带其他form参数上传
*
* @param url
* @param callback
* @param file
* @param fileKey
* @param params
* @throws IOException
*/
private void _postAsyn(String url, ResultCallback callback, File file, String fileKey, Param... params) throws IOException
{
Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, params);
deliveryResult(callback, request);
}

/**
* 异步下载文件
*
* @param url
* @param destFileDir 本地文件存储的文件夹
* @param callback
*/
private void _downloadAsyn(final String url, final String destFileDir, final ResultCallback callback)
{
final Request request = new Request.Builder()
.url(url)
.build();
final Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback()
{
@Override
public void onFailure(final Request request, final IOException e)
{
sendFailedStringCallback(request, e, callback);
}

@Override
public void onResponse(Response response)
{
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try
{
is = response.body().byteStream();
File file = new File(destFileDir, getFileName(url));
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1)
{
fos.write(buf, 0, len);
}
fos.flush();
//如果下载文件成功,第一个参数为文件的绝对路径
sendSuccessResultCallback(file.getAbsolutePath(), callback);
} catch (IOException e)
{
sendFailedStringCallback(response.request(), e, callback);
} finally
{
try
{
if (is != null) is.close();
} catch (IOException e)
{
}
try
{
if (fos != null) fos.close();
} catch (IOException e)
{
}
}

}
});
}

private String getFileName(String path)
{
int separatorIndex = path.lastIndexOf("/");
return (separatorIndex < 0) ? path : path.substring(separatorIndex + 1, path.length());
}

/**
* 加载图片
*
* @param view
* @param url
* @throws IOException
*/
private void _displayImage(final ImageView view, final String url, final int errorResId)
{
final Request request = new Request.Builder()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback()
{
@Override
public void onFailure(Request request, IOException e)
{
setErrorResId(view, errorResId);
}

@Override
public void onResponse(Response response)
{
InputStream is = null;
try
{
is = response.body().byteStream();
ImageUtils.ImageSize actualImageSize = ImageUtils.getImageSize(is);
ImageUtils.ImageSize imageViewSize = ImageUtils.getImageViewSize(view);
int inSampleSize = ImageUtils.calculateInSampleSize(actualImageSize, imageViewSize);
try
{
is.reset();
} catch (IOException e)
{
response = _getAsyn(url);
is = response.body().byteStream();
}

BitmapFactory.Options ops = new BitmapFactory.Options();
ops.inJustDecodeBounds = false;
ops.inSampleSize = inSampleSize;
final Bitmap bm = BitmapFactory.decodeStream(is, null, ops);
mDelivery.post(new Runnable()
{
@Override
public void run()
{
view.setImageBitmap(bm);
}
});
} catch (Exception e)
{
setErrorResId(view, errorResId);

} finally
{
if (is != null) try
{
is.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
});


}

private void setErrorResId(final ImageView view, final int errorResId)
{
mDelivery.post(new Runnable()
{
@Override
public void run()
{
view.setImageResource(errorResId);
}
});
}


//*************对外公布的方法************


public static Response getAsyn(String url) throws IOException
{
return getInstance()._getAsyn(url);
}


public static String getAsString(String url) throws IOException
{
return getInstance()._getAsString(url);
}

public static void getAsyn(String url, ResultCallback callback)
{
getInstance()._getAsyn(url, callback);
}

public static Response post(String url, Param... params) throws IOException
{
return getInstance()._post(url, params);
}

public static String postAsString(String url, Param... params) throws IOException
{
return getInstance()._postAsString(url, params);
}

public static void postAsyn(String url, final ResultCallback callback, Param... params)
{
getInstance()._postAsyn(url, callback, params);
}


public static void postAsyn(String url, final ResultCallback callback, Map<String, String> params)
{
getInstance()._postAsyn(url, callback, params);
}


public static Response post(String url, File[] files, String[] fileKeys, Param... params) throws IOException
{
return getInstance()._post(url, files, fileKeys, params);
}

public static Response post(String url, File file, String fileKey) throws IOException
{
return getInstance()._post(url, file, fileKey);
}

public static Response post(String url, File file, String fileKey, Param... params) throws IOException
{
return getInstance()._post(url, file, fileKey, params);
}

public static void postAsyn(String url, ResultCallback callback, File[] files, String[] fileKeys, Param... params) throws IOException
{
getInstance()._postAsyn(url, callback, files, fileKeys, params);
}


public static void postAsyn(String url, ResultCallback callback, File file, String fileKey) throws IOException
{
getInstance()._postAsyn(url, callback, file, fileKey);
}


public static void postAsyn(String url, ResultCallback callback, File file, String fileKey, Param... params) throws IOException
{
getInstance()._postAsyn(url, callback, file, fileKey, params);
}

public static void displayImage(final ImageView view, String url, int errorResId) throws IOException
{
getInstance()._displayImage(view, url, errorResId);
}


public static void displayImage(final ImageView view, String url)
{
getInstance()._displayImage(view, url, -1);
}

public static void downloadAsyn(String url, String destDir, ResultCallback callback)
{
getInstance()._downloadAsyn(url, destDir, callback);
}

//****************************


private Request buildMultipartFormRequest(String url, File[] files,
String[] fileKeys, Param[] params)
{
params = validateParam(params);

MultipartBuilder builder = new MultipartBuilder()
.type(MultipartBuilder.FORM);

for (Param param : params)
{
builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + param.key + "\""),
RequestBody.create(null, param.value));
}
if (files != null)
{
RequestBody fileBody = null;
for (int i = 0; i < files.length; i++)
{
File file = files[i];
String fileName = file.getName();
fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileName)), file);
//TODO 根据文件名设置contentType
builder.addPart(Headers.of("Content-Disposition",
"form-data; name=\"" + fileKeys[i] + "\"; filename=\"" + fileName + "\""),
fileBody);
}
}

RequestBody requestBody = builder.build();
return new Request.Builder()
.url(url)
.post(requestBody)
.build();
}

private String guessMimeType(String path)
{
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String contentTypeFor = fileNameMap.getContentTypeFor(path);
if (contentTypeFor == null)
{
contentTypeFor = "application/octet-stream";
}
return contentTypeFor;
}


private Param[] validateParam(Param[] params)
{
if (params == null)
return new Param[0];
else return params;
}

private Param[] map2Params(Map<String, String> params)
{
if (params == null) return new Param[0];
int size = params.size();
Param[] res = new Param[size];
Set<Map.Entry<String, String>> entries = params.entrySet();
int i = 0;
for (Map.Entry<String, String> entry : entries)
{
res[i++] = new Param(entry.getKey(), entry.getValue());
}
return res;
}

private static final String SESSION_KEY = "Set-Cookie";
private static final String mSessionKey = "JSESSIONID";

private Map<String, String> mSessions = new HashMap<String, String>();

private void deliveryResult(final ResultCallback callback, Request request)
{
mOkHttpClient.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(final Request request, final IOException e)
{
sendFailedStringCallback(request, e, callback);
}

@Override
public void onResponse(final Response response)
{
try
{
final String string = response.body().string();
if (callback.mType == String.class)
{
sendSuccessResultCallback(string, callback);
} else
{
Object o = mGson.fromJson(string, callback.mType);
sendSuccessResultCallback(o, callback);
}


} catch (IOException e)
{
sendFailedStringCallback(response.request(), e, callback);
} catch (com.google.gson.JsonParseException e)//Json解析的错误
{
sendFailedStringCallback(response.request(), e, callback);
}

}
});
}

private void sendFailedStringCallback(final Request request, final Exception e, final ResultCallback callback)
{
mDelivery.post(new Runnable()
{
@Override
public void run()
{
if (callback != null)
callback.onError(request, e);
}
});
}

private void sendSuccessResultCallback(final Object object, final ResultCallback callback)
{
mDelivery.post(new Runnable()
{
@Override
public void run()
{
if (callback != null)
{
callback.onResponse(object);
}
}
});
}

private Request buildPostRequest(String url, Param[] params)
{
if (params == null)
{
params = new Param[0];
}
FormEncodingBuilder builder = new FormEncodingBuilder();
for (Param param : params)
{
builder.add(param.key, param.value);
}
RequestBody requestBody = builder.build();
return new Request.Builder()
.url(url)
.post(requestBody)
.build();
}


public static abstract class ResultCallback<T>
{
Type mType;

public ResultCallback()
{
mType = getSuperclassTypeParameter(getClass());
}

static Type getSuperclassTypeParameter(Class<?> subclass)
{
Type superclass = subclass.getGenericSuperclass();
if (superclass instanceof Class)
{
throw new RuntimeException("Missing type parameter.");
}
ParameterizedType parameterized = (ParameterizedType) superclass;
return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
}

public abstract void onError(Request request, Exception e);

public abstract void onResponse(T response);
}

public static class Param
{
public Param()
{
}

public Param(String key, String value)
{
this.key = key;
this.value = value;
}

String key;
String value;
}


}

参考文献: