OkHttp,Retrofit 1.x - 2.x 基本使用

 Square 为广大开发者奉献了OkHttp,Retrofit1.x,Retrofit2.x,运用比较广泛,这三个工具有很多相似之处,初学者可能会有一些使用迷惑。这里来总结一下它们的一些基本使用和一些细微差别。

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
/**************
Retrofit 基本使用方法
 
Retrofit 到底是返回什么? void, Observable, Call?
 
*************/
/********************************************Retrofit****************************************************************/
/*** 同步调用的方式  ****/
interface  GitHubService {
   @GET ( "/repos/{owner}/{repo}/contributors" )
   List<Contributor> repoContributors(
       @Path ( "owner" ) String owner,
       @Path ( "repo" ) String repo);
}
 
List<Contributor> contributors =
     gitHubService.repoContributors( "square" "retrofit" );
/***** 异步调用的方式 仅限于 Retrofit 1.x !!!!!!! *****/
interface  GitHubService {
   @GET ( "/repos/{owner}/{repo}/contributors" )
   void  repoContributors(
       @Path ( "owner" ) String owner,
       @Path ( "repo" ) String repo,
       Callback<List<Contributor>> cb);  // 异步调用添加 CallBack
}
 
service.repoContributors( "square" "retrofit" new  Callback<List<Contributor>>() {
   @Override  void  success(List<Contributor> contributors, Response response) {
     // ...
   }
 
 
   @Override  void  failure(RetrofitError error) {
     // ...
   }
});
 
/**** Rxjava 方式 ****/
interface  GitHubService {
   @GET ( "/repos/{owner}/{repo}/contributors" )
   Observable<List<Contributor>> repoContributors(
       @Path ( "owner" ) String owner,
       @Path ( "repo" ) String repo);
}
// 调用
gitHubService.repoContributors( "square" "retrofit" )
     .subscribe( new  Action1<List<Contributor>>() {
       @Override  public  void  call(List<Contributor> contributors) {
         // ...
       }
     });
     
/*******************************注意以下三个Callback的不同***************************************/
     
// Retrofit Callback Version 1.9
public  interface  Callback<T> {
 
   /** Successful HTTP response. */
   void  success(T t, Response response);
 
   /**
    * Unsuccessful HTTP response due to network failure, non-2XX status code, or unexpected
    * exception.
    */
   void  failure(RetrofitError error);
}
// Retrofit Callback Version 2.0    !!!!!!!!!
public  interface  Callback<T> {
   /** Successful HTTP response. */
   void  onResponse(Response<T> response, Retrofit retrofit);
 
   /** Invoked when a network or unexpected exception occurred during the HTTP request. */
   void  onFailure(Throwable t);
}
// OkHttp  
public  interface  Callback {
   void  onFailure(Request request, IOException e);
 
   void  onResponse(Response response)  throws  IOException;  // 注意参数不同
}
 
 
 
/*********************************回顾一下Okhttp的调用方式********************************************/
 
//1. 创建
OkHttpClient : OkHttpClient client =  new  OkHttpClient();
//2. 创建
Request :  Request request =  new  Request.Builder()
                                         .url( "https://api.github.com/repos/square/okhttp/issues" )
                                         .header( "User-Agent" "OkHttp Headers.java" )
                                         .addHeader( "Accept" "application/json; q=0.5" )
                                         .addHeader( "Accept" "application/vnd.github.v3+json" )
                                         .build();
                                                 
//3. 使用 client 执行请求(两种方式): 
//第一种,同步执行
Response response = client.newCall(request).execute();
// 第二种,异步执行方式
client.newCall(request).enqueue( new  Callback() {
     @Override
     public  void  onFailure(Request request, Throwable throwable) {
     // 复写该方法
                                              
     }
     @Override  public  void  onResponse(Response response)  throws  IOException {
     // 复写该方法
     }                                     
}
     
     
/***********************************Retrofit 1.0 不能获得 Header 或者整个 Body*****************************************/
/**********2.x 引入 Call , 每个Call只能调用一次,可以使用Clone方法来生成一次调用多次,使用Call既可以同步也可以异步*********/
 
interface  GitHubService {
   @GET ( "/repos/{owner}/{repo}/contributors" )
   Call<List<Contributor>> repoContributors(
       @Path ( "owner" ) String owner,
       @Path ( "repo" ) String repo);
}
 
Call<List<Contributor>> call =
     gitHubService.repoContributors( "square" "retrofit" );
 
response = call.execute();  /*************** 同步的方式调用,注意这里返回了 Response 后面会提到 ********************/
 
// This will throw IllegalStateException: 每个Call只能执行一次
response = call.execute();
 
Call<List<Contributor>> call2 = call.clone();  // 调用Clone之后又可以执行
// This will not throw:
response = call2.execute();
 
/************************ 异步的方式调用 *********************************/
 
Call<List<Contributor>> call =
     gitHubService.repoContributors( "square" "retrofit" );
 
call.enqueue( new  Callback<List<Contributor>>() {
   @Override  void  onResponse( /* ... */ ) {
     // ...
   }
 
   @Override  void  onFailure(Throwable t) {
     // ...
   }
});
 
/****************************引入 Response,获取返回的RawData,包括:response code, response message, headers**********************************/
 
class  Response<T> {
   int  code();
   String message();
   Headers headers();
 
   boolean  isSuccess();
   T body();
   ResponseBody errorBody();
   com.squareup.okhttp.Response raw();
}
 
interface  GitHubService {
   @GET ( "/repos/{owner}/{repo}/contributors" )
   Call<List<Contributor>> repoContributors(
       @Path ( "owner" ) String owner,
       @Path ( "repo" ) String repo);
}
 
Call<List<Contributor>> call =
     gitHubService.repoContributors( "square" "retrofit" );
Response<List<Contributor>> response = call.execute();
 
/*********************************** Dynamic URL *****************************************/
 
interface  GitHubService {
   @GET ( "/repos/{owner}/{repo}/contributors" )
   Call<List<Contributor>> repoContributors(
       @Path ( "owner" ) String owner,
       @Path ( "repo" ) String repo);
 
   @GET
   Call<List<Contributor>> repoContributorsPaginate(
       @Url  String url); // 直接填入 URL 而不是在GET中替换字段的方式
}
 
/*************************************根据返回值实现重载*****************************************************/
interface  SomeService {
   @GET ( "/some/proto/endpoint" )
   Call<SomeProtoResponse> someProtoEndpoint();  // SomeProtoResponse
 
   @GET ( "/some/json/endpoint" )
   Call<SomeJsonResponse> someJsonEndpoint();  // SomeJsonResponse
}
 
interface  GitHubService {
   @GET ( "/repos/{owner}/{repo}/contributors" )
   Call<List<Contributor>> repoContributors(..);
 
   @GET ( "/repos/{owner}/{repo}/contributors" )
   Observable<List<Contributor>> repoContributors2(..);
 
   @GET ( "/repos/{owner}/{repo}/contributors" )
   Future<List<Contributor>> repoContributors3(..);  // 可以返回 Future
}
 
/******************************************Retrofit 1.x Interceptor,添加头部信息的时候经常用到Interceptor*************************************************************/
     RestAdapter.Builder builder =  new  RestAdapter.Builder().setRequestInterceptor( new  RequestInterceptor() {
         @Override
         public  void  intercept(RequestFacade request) {
             request.addHeader( "Accept" "application/json;versions=1" );
         }
     });
 
 
/******************************************Retrofit 2.x Interceptor**************************************************/           
     
OkHttpClient client =  new  OkHttpClient();
client.interceptors().add( new  Interceptor() {   
     @Override
     public  Response intercept(Chain chain)  throws  IOException {
         Request original = chain.request();
         
         Request request = original.newBuilder()
                                   .header( "Accept" "application/json" )
                                   .header( "Authorization" "auth-token" )
                                   .method(original.method(), original.body())
                                   .build();
        
        Response response = chain.proceed(request);
        return  response;     
         
     }  
}
 
Retrofit retrofit = Retrofit.Builder()
             .baseUrl( "https://your.api.url/v2/" )
             .client(client).build();
 
 
/***************************************异步实例*********************************************/
public  interface  APIService {
 
     @GET ( "/users/{user}/repos" )
     Call<List<Repo>> listRepos( @Path ( "user" ) String user);
 
     @GET ( "/users/{user}/repos" )
     Call<String> listReposStr( @Path ( "user" ) String user);
//错误,不能这样使用异步
//    @GET("/users/{user}/repos")
//    void listRepos(@Path("user") String user, Callback<List<Repo>> callback);
}
 
private  void  prepareServiceAPI() {
     //For logging
     HttpLoggingInterceptor logging =  new  HttpLoggingInterceptor();
     logging.setLevel(HttpLoggingInterceptor.Level.BODY);
 
     OkHttpClient client =  new  OkHttpClient();
     client.interceptors().add( new  MyInterceptor());
     client.interceptors().add(logging);
     // setUp Retrofit
     Retrofit retrofit =  new  Retrofit.Builder()
             .baseUrl( "https://api.github.com" )
             //.addConverterFactory(new ToStringConverterFactory())
             .addConverterFactory(GsonConverterFactory.create())
             .client(client)
             .build();
 
     service = retrofit.create(APIService. class );
}
// 异步调用
public  void  execute() {
     Call<List<Repo>> call = service.listRepos( "pasha656" );
     call.enqueue( new  Callback<List<Repo>>() {
         @Override
         public  void  onResponse(Response<List<Repo>> response, Retrofit retrofit) {
 
             if  (response.isSuccess()) {
                 if  (!response.body().isEmpty()) {
                     StringBuilder sb =  new  StringBuilder();
                     for  (Repo r : response.body()) {
                         sb.append(r.getId()).append( " " ).append(r.getName()).append( " \n" );
                     }
                     activity.setText(sb.toString());
                 }
             else  {
                 APIError error = ErrorUtils.parseError(response, retrofit);
                 Log.d( "Pasha" "No succsess message " +error.getMessage());
             }
 
 
             if  (response.errorBody() !=  null ) {
                 try  {
                     Log.d( "Pasha" "Error " +response.errorBody().string());
                 catch  (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
 
         @Override
         public  void  onFailure(Throwable t) {
             Log.d( "Pasha" "onFailure " +t.getMessage());
         }
     });
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值