OkHttp升级OkHttp3的姿势全解

1、包名改变

之前

compile com.squareup.okhttp:okhttp:2.5.0

之后

implementation com.squareup.okhttp3:okhttp:3.14.1

2、OkHttpClient参数配置

之前

mOkHttpClient.setCache(cache) 这样设置

之后

需要在OkHttpClient.Builder上设置可配置  

3、Timeout参数配置

之前

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(10000, TimeUnit.MILLISECONDS);
okHttpClient.setConnectTimeout(15000, TimeUnit.MILLISECONDS);

之后

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newBuilder().readTimeout(10000, TimeUnit.MILLISECONDS);
okHttpClient.newBuilder().connectTimeout(15000, TimeUnit.MILLISECONDS);

4、FormEncodingBuilder参数配置

之前

Request.Builder builder = new Request.Builder();
FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
formEncodingBuilder.add(key, value);
RequestBody requestBody = formEncodingBuilder.build();
builder.url(this.url).post(requestBody);

之后

Request.Builder builder = new Request.Builder();
FormBody formEncodingBuilder = new FormBody.Builder().add(key, value).build();
builder.url(this.url).post(formEncodingBuilder);

5、Call参数配置

之前

OkHttpClient mOkHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder().url();
Request request = builder.build(); 
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) { 
            }
            @Override
            public void onResponse(Response response) throws IOException { 
            }
        });

之后

OkHttpClient mOkHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder().url();
Request request = builder.build(); 
Call call = mOkHttpClient.newCall(request);
 call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });

6、Cancel方法参数

3.0之前我们去掉call 可以直接使用mOkHttpClient.cancel(tag);
3.0之后直接阉割掉此方法我们可以采用如下方法:

public void cancelCallsWithTag(Object tag) {  
        checkInit();  
        if (tag == null) {  
            return;  
        }  
        synchronized (mOkHttpClient.dispatcher().getClass()) {  
            for (Call call : mOkHttpClient.dispatcher().queuedCalls()) {  
                if (tag.equals(call.request().tag())) call.cancel();  
            }  
            for (Call call : mOkHttpClient.dispatcher().runningCalls()) {  
                if (tag.equals(call.request().tag())) call.cancel();  
            }  
        }  
    }

7、对https支持的不同

okhttp默认调用了getDefaultSSLSocketFactory方法,该方法提供了默认的SSLSocketFactory,就算不设置SSLSocketFactory也可以支持https,setSslSocketFactory没有做非空判断,如果设置为空,则使用默认的SSLSocketFactory。
okhttp3设置https的方法sslSocketFactory,对SSLSocketFactory做了非空判断,为空会抛出异常。如果不主动设置SSLSocketFactory,okhttp3也提供了默认的http3支持。

if (builder.sslSocketFactory != null || !isTLS) { 
    this.sslSocketFactory = builder.sslSocketFactory; 
  } else { 
     try { 
      SSLContext sslContext = SSLContext.getInstance("TLS"); 
       sslContext.init(null, null, null); 
       this.sslSocketFactory = sslContext.getSocketFactory(); 
    } catch (GeneralSecurityException e) { 
       throw new AssertionError(); // The system has no TLS. Just give up. 
    } 
}
    原文作者:我们都很努力着
    原文地址: https://www.jianshu.com/p/12b4ea94dfb3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞