一、总体架构
截图.png
二、源码分析
1.OkHttpClient client = new OkHttpClient(); // 创建OkHttpClient对象
2.Request request = new Request.Builder() // 用Request.Builder()创建请求
.url(url)
.build();
3.Response response = client.newCall(request).execute(); // 把请求传入newCall方法中,然后执行,就能拿到你想要的返回值
1.1 OkHttpClient
public class OkHttpClient implements Cloneable, Call.Factory, WebSocket.Factory {
public OkHttpClient() {
this(new Builder());
}
OkHttpClient(Builder builder) {
this.dispatcher = builder.dispatcher; // 分发器
this.proxy = builder.proxy; //
this.protocols = builder.protocols; // 池子
this.connectionSpecs = builder.connectionSpecs; //
this.interceptors = Util.immutableList(builder.interceptors); // 拦截器 (属于应用层拦截器)
this.networkInterceptors = Util.immutableList(builder.networkInterceptors); // 网络拦截器
this.eventListenerFactory = builder.eventListenerFactory; //
this.proxySelector = builder.proxySelector; //
this.cookieJar = builder.cookieJar; //
this.cache = builder.cache; //
this.internalCache = builder.internalCache; //
this.socketFactory = builder.socketFactory; //
boolean isTLS = false;
......
this.hostnameVerifier = builder.hostnameVerifier;
this.certificatePinner = builder.certificatePinner.withCertificateChainCleaner(
certificateChainCleaner);
this.proxyAuthenticator = builder.proxyAuthenticator;
this.authenticator = builder.authenticator;
this.connectionPool = builder.connectionPool; // 连接池
this.dns = builder.dns;
this.followSslRedirects = builder.followSslRedirects;
this.followRedirects = builder.followRedirects;
this.retryOnConnectionFailure = builder.retryOnConnectionFailure;
this.connectTimeout = builder.connectTimeout; // 连接时间
this.readTimeout = builder.readTimeout; // 读取时间
this.writeTimeout = builder.writeTimeout; // 写入时间
this.pingInterval = builder.pingInterval;
}
}
网友评论