44import com .qiniu .common .QiniuException ;
55import com .qiniu .util .StringMap ;
66import com .qiniu .util .StringUtils ;
7- import com . squareup . okhttp .*;
7+ import okhttp3 .*;
88import okio .BufferedSink ;
99
1010import java .io .File ;
@@ -25,25 +25,31 @@ public Client() {
2525 Dispatcher dispatcher = new Dispatcher ();
2626 dispatcher .setMaxRequests (64 );
2727 dispatcher .setMaxRequestsPerHost (16 );
28- ConnectionPool connectionPool = new ConnectionPool (32 , 5 * 60 * 1000 );
29- httpClient = new OkHttpClient ();
30- httpClient .setDispatcher (dispatcher );
31- httpClient .setConnectionPool (connectionPool );
32- httpClient .networkInterceptors ().add (new Interceptor () {
28+ ConnectionPool connectionPool = new ConnectionPool (32 , 5 , TimeUnit .MINUTES );
29+ OkHttpClient .Builder builder = new OkHttpClient .Builder ();
30+
31+ builder .dispatcher (dispatcher );
32+ builder .connectionPool (connectionPool );
33+ builder .addNetworkInterceptor (new Interceptor () {
3334 @ Override
34- public com . squareup . okhttp .Response intercept (Chain chain ) throws IOException {
35+ public okhttp3 .Response intercept (Chain chain ) throws IOException {
3536 Request request = chain .request ();
3637
37- com . squareup . okhttp .Response response = chain .proceed (request );
38+ okhttp3 .Response response = chain .proceed (request );
3839 IpTag tag = (IpTag ) request .tag ();
39- String ip = chain .connection ().getSocket ().getRemoteSocketAddress ().toString ();
40- tag .ip = ip ;
40+ try {
41+ tag .ip = chain .connection ().socket ().getRemoteSocketAddress ().toString ();
42+ } catch (Exception e ) {
43+ e .printStackTrace ();
44+ tag .ip = "" ;
45+ }
4146 return response ;
4247 }
4348 });
44- httpClient .setConnectTimeout (Config .CONNECT_TIMEOUT , TimeUnit .SECONDS );
45- httpClient .setReadTimeout (Config .RESPONSE_TIMEOUT , TimeUnit .SECONDS );
46- httpClient .setWriteTimeout (Config .WRITE_TIMEOUT , TimeUnit .SECONDS );
49+ builder .connectTimeout (Config .CONNECT_TIMEOUT , TimeUnit .SECONDS );
50+ builder .readTimeout (Config .RESPONSE_TIMEOUT , TimeUnit .SECONDS );
51+ builder .writeTimeout (Config .WRITE_TIMEOUT , TimeUnit .SECONDS );
52+ httpClient = builder .build ();
4753 }
4854
4955 private static String userAgent () {
@@ -94,7 +100,7 @@ public Response post(String url, String body, StringMap headers) throws QiniuExc
94100 }
95101
96102 public Response post (String url , StringMap params , StringMap headers ) throws QiniuException {
97- final FormEncodingBuilder f = new FormEncodingBuilder ();
103+ final FormBody . Builder f = new FormBody . Builder ();
98104 params .forEach (new StringMap .Consumer () {
99105 @ Override
100106 public void accept (String key , Object value ) {
@@ -160,7 +166,7 @@ private Response multipartPost(String url,
160166 String fileName ,
161167 RequestBody file ,
162168 StringMap headers ) throws QiniuException {
163- final MultipartBuilder mb = new MultipartBuilder ();
169+ final MultipartBody . Builder mb = new MultipartBody . Builder ();
164170 mb .addFormDataPart (name , fileName , file );
165171
166172 fields .forEach (new StringMap .Consumer () {
@@ -169,7 +175,7 @@ public void accept(String key, Object value) {
169175 mb .addFormDataPart (key , value .toString ());
170176 }
171177 });
172- mb .type (MediaType .parse ("multipart/form-data" ));
178+ mb .setType (MediaType .parse ("multipart/form-data" ));
173179 RequestBody body = mb .build ();
174180 Request .Builder requestBuilder = new Request .Builder ().url (url ).post (body );
175181 return send (requestBuilder , headers );
@@ -187,7 +193,7 @@ public void accept(String key, Object value) {
187193
188194 requestBuilder .header ("User-Agent" , userAgent ());
189195 long start = System .currentTimeMillis ();
190- com . squareup . okhttp .Response res = null ;
196+ okhttp3 .Response res = null ;
191197 Response r ;
192198 double duration = (System .currentTimeMillis () - start ) / 1000.0 ;
193199 IpTag tag = new IpTag ();
@@ -220,14 +226,14 @@ public void accept(String key, Object value) {
220226 IpTag tag = new IpTag ();
221227 httpClient .newCall (requestBuilder .tag (tag ).build ()).enqueue (new Callback () {
222228 @ Override
223- public void onFailure (Request request , IOException e ) {
229+ public void onFailure (Call call , IOException e ) {
224230 e .printStackTrace ();
225231 long duration = (System .currentTimeMillis () - start ) / 1000 ;
226232 cb .complete (Response .createError (null , "" , duration , e .getMessage ()));
227233 }
228234
229235 @ Override
230- public void onResponse (com . squareup . okhttp .Response response ) throws IOException {
236+ public void onResponse (Call call , okhttp3 .Response response ) throws IOException {
231237 long duration = (System .currentTimeMillis () - start ) / 1000 ;
232238 cb .complete (Response .create (response , "" , duration ));
233239 }
@@ -279,7 +285,7 @@ private void asyncMultipartPost(String url,
279285 RequestBody file ,
280286 StringMap headers ,
281287 AsyncCallback cb ) {
282- final MultipartBuilder mb = new MultipartBuilder ();
288+ final MultipartBody . Builder mb = new MultipartBody . Builder ();
283289 mb .addFormDataPart (name , fileName , file );
284290
285291 fields .forEach (new StringMap .Consumer () {
@@ -288,7 +294,7 @@ public void accept(String key, Object value) {
288294 mb .addFormDataPart (key , value .toString ());
289295 }
290296 });
291- mb .type (MediaType .parse ("multipart/form-data" ));
297+ mb .setType (MediaType .parse ("multipart/form-data" ));
292298 RequestBody body = mb .build ();
293299 Request .Builder requestBuilder = new Request .Builder ().url (url ).post (body );
294300 asyncSend (requestBuilder , headers , cb );
0 commit comments