赞
踩
OkHttp现在应该算是最火的Http第三方库,Retrofit底层也是使用OkHttp,网上很多教程都写的不错,但是有些我认为重要的知识,大多一笔带过,所以我决定写一篇入门文章
OkHttp官网地址:http://square.github.io/okhttp/
OkHttp GitHub地址:https://github.com/square/okhttp
OkHttp的源码已经新鲜出炉,简单易懂,赶紧学习起来
框架和流程——OkHttp 源码详解(一)
ConnectInterceptor 解析——OkHttp 源码详解(二)
网络访问的高效性要求,可以说是为高效而生
Requests(请求)
每一个HTTP请求中都应该包含一个URL,一个GET或POST方法以及Header或其他参数,当然还可以含特定内容类型的数据流。
Responses(响应)
响应则包含一个回复代码(200代表成功,404代表未找到),Header和定制可选的body。
compile 'com.squareup.okhttp3:okhttp:3.2.0'
简单来说,通过OkHttpClient可以发送一个Http请求,并读取该Http请求的响应,它是一个生产Call的工厂。
此外,受益于一个共享的响应缓存/线程池/复用的连接等因素,绝大多数应用使用一个OkHttpClient实例,便可以满足整个应用的Http请求。
三种创建实例的方法:
OkHttpClient client = new OkHttpClient();
OkHttpClient clientWith30sTimeout = client.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.build();
OkHttpClient client = client.newBuilder().build();
看一下OkHttpClient的源码,会发现缓存/代理等等需求,一应俱全的按照类封装到了Builder中。
Dispatcher dispatcher; // 分发 Proxy proxy; // 代理 List<Protocol> protocols; List<ConnectionSpec> connectionSpecs; final List<Interceptor> interceptors = new ArrayList<>(); // 拦截器 final List<Interceptor> networkInterceptors = new ArrayList<>(); // 网络拦截器 ProxySelector proxySelector; CookieJar cookieJar; Cache cache; // 缓存 InternalCache internalCache; SocketFactory socketFactory; SSLSocketFactory sslSocketFactory; HostnameVerifier hostnameVerifier; CertificatePinner certificatePinner; Authenticator proxyAuthenticator; // 代理证书 Authenticator authenticator; // 证书 ConnectionPool connectionPool; Dns dns; // DNS boolean followSslRedirects; boolean followRedirects; boolean retryOnConnectionFailure; int connectTimeout; int readTimeout; int writeTimeout;
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
简单看一下Request类,可以发现它代表一个Http请求,需要注意的是Request一旦build()之后,便不可修改。
主要通过new Request.Builder()来一步一步构造的。看一下Builder的代码。
public Builder() {
this.method = "GET";
this.headers = new Headers.Builder();
}
默认是Get方法,
此外还创建了头信息。Headers类中是通过List<String> namesAndValues = new ArrayList<>(20),来存放头信息的,一开始我也很纳闷,头信息都是一对一对的为什么要用List,看一下源码发现,在存取的时候都是将索引+2或者-2。并且头信息可以存在多个相同的Key信息。
跟到newCall()方法中发现,又使用OkHttpClient实例和Request的实例,一起构造了一个RealCall的实例。
RealCall类简单做了一个托管并通过Dispather类对请求进行分发和执行,实际开启线程发起请求的方法就在这个类中。
随后又调用execute()方法,拿到了一个响应。这个execute()方法,实际上执行的就是RealCall中的execute()方法,最后调用了Dispatcher的execute()方法。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。