https://github.com/square/okhttp/wiki/Calls
OkHttpClient.Builder:OkHttpClient可通过Builder采用建造者模式构建,通过Builder可以方便灵活的设置通用参数
由上面的设计图可知,OkhttpClient是核心,他就像一个浏览器一样,什么工作,比如请求,接受响应都是由它做的,关键就是怎么给他配置http协议的那些配置了,因为配置太多,所以用了Builder帮助配置
翻翻Builder的源码可以知道,即时你没有配置啥参数,Builder也为你默认设置一些参数,比如他默认设置支持 Protocol.HTTP_2, Protocol.SPDY_3, Protocol.HTTP_1_1这三种协议,默认连接,读写超时都是10s,DNS用系统的,默认失败重连。。。。
Dispatcher dispatcher;
Proxy proxy;
List protocols;
List connectionSpecs;
final List interceptors = new ArrayList<>();
final List networkInterceptors = new ArrayList<>();
ProxySelector proxySelector;
CookieJar cookieJar;
Cache cache;
InternalCache internalCache;
SocketFactory socketFactory;
SSLSocketFactory sslSocketFactory;
CertificateChainCleaner certificateChainCleaner;
HostnameVerifier hostnameVerifier;
CertificatePinner certificatePinner;
Authenticator proxyAuthenticator;
Authenticator authenticator;
ConnectionPool connectionPool;
Dns dns;
boolean followSslRedirects;
boolean followRedirects;
boolean retryOnConnectionFailure;
int connectTimeout;
int readTimeout;
int writeTimeout;
public Builder() {
dispatcher = new Dispatcher();
protocols = DEFAULT_PROTOCOLS;
connectionSpecs = DEFAULT_CONNECTION_SPECS;
proxySelector = ProxySelector.getDefault();
cookieJar = CookieJar.NO_COOKIES;
socketFactory = SocketFactory.getDefault();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
certificatePinner = CertificatePinner.DEFAULT;
proxyAuthenticator = Authenticator.NONE;
authenticator = Authenticator.NONE;
connectionPool = new ConnectionPool();
dns = Dns.SYSTEM;
followSslRedirects = true;
followRedirects = true;
retryOnConnectionFailure = true;
connectTimeout = 10_000;
readTimeout = 10_000;
writeTimeout = 10_000;
}
因为只要有请求就必须有okhttpclient参与,一般项目是分模块的,每个模块基本都有网络请求,总不能每个请求都要构造一个okhttpclient去请求吧,所以okhttpclient设计为单例,不用new ,到处都可以用它,方便快捷,代码得到了复用,也节省了内存开销
这里采用内部静态类实现单例
public class OkhttpHelper {
public static final int DEFAULTITMEOUT=5;
private OkHttpClient okHttpClient;
private OkhttpHelper(){
OkHttpClient.Builder builder=new OkHttpClient.Builder()
.connectTimeout(DEFAULTITMEOUT, TimeUnit.SECONDS);
okHttpClient=builder.build();
}
public OkHttpClient getOkHttpClient() {
return okHttpClient;
}
private static class OkhttpHelperHolder{
public static OkhttpHelper INSTANCE=new OkhttpHelper();
}
}
有Http协议,每个连接必有请求头,是供服务端或客户端读取的,这里Okhttp封装了请求,并且也为它提供builder类,再翻翻看
这些是默认的参数
private HttpUrl url;
private String method;
private Headers.Builder headers;
private RequestBody body;
private Object tag;
public Builder() {
this.method = "GET";
this.headers = new Headers.Builder();
}
private Builder(Request request) {
this.url = request.url;
this.method = request.method;
this.body = request.body;
this.tag = request.tag;
this.headers = request.headers.newBuilder();
}
public Builder cacheControl(CacheControl cacheControl) {
String value = cacheControl.toString();
if (value.isEmpty()) return removeHeader("Cache-Control");
return header("Cache-Control", value);
}
其实它的具体实现是调用method()方法
public Builder get() {
return method("GET", null);
}
HTTP客户端的工作是接受您的请求并产生响应。这在理论上很简单,但在实践中变得棘手。
每个HTTP请求包含一个URL,一个请求方法(像GET,POST),和一个头部列表,请求也包含一个body:特殊内容类型的数据流,比如文件,json数据...
响应使用代码(如200获得成功或404找不到)来应答请求,标题及其自己的可选正文。
当你提供给OKhttp一个HTTP请求,他就会按你定义的请求去执行,重写请求上面已给出了源码,直接用builder去set就是,可见okhttp支持很多header,如Content-Length, Transfer-Encoding, User-Agent, Host, Connection, and Content-Type
响应是okhttp制造的,我们只是处理响应就行了。
当您请求的URL已经移动时,网络服务器将返回一个302类型的响应代码,以指示该文档的新URL。 OkHttp将遵循重定向来检索最终的响应。
有时连接失败:池连接过时并断开连接,或者无法访问Web服务器本身。 OkHttp将使用不同的路由重试该请求(如果有)。
通过重写,重定向,后续跟踪和重试,您的简单请求可能会产生许多请求和响应,OkHttp使用Call来建立满足您的请求的任务,但是需要许多中间请求和响应。
有两种执行呼叫的方式
对于同步调用,您可以自己创建线程,并负责管理同时发出的请求数。同时连接太多浪费资源;太少的危害延迟。在安卓开发显然不能阻塞主线程,得在另一个线程执行。
对于异步调用,Dispatcher实现最大同时请求的策略。您可以设置每个网络服务器的最大值(默认值为5),总体(默认值为64)。
1.它使用URL并配置了OkHttpClient来创建一个地址。该地址指定我们如何连接到Web服务器。
2.它尝试从连接池检索与该地址的连接.
3.如果在池中没有找到连接,则会选择尝试的路由。这通常意味着进行DNS请求以获取服务器的IP地址。然后,如果需要,可以选择TLS版本和代理服务器。
4.如果它是一个新路由,它通过构建直接套接字连接,TLS隧道(通过HTTP代理的HTTPS)或直接TLS连接进行连接。它需要TLS握手。
5.它发送HTTP请求并读取响应
如果连接有问题,OkHttp将选择另一个路由,然后重试。这允许OkHttp在服务器地址的子集不可达时恢复。
在接收到响应后,连接将返回到池中,以便将来可以重用该请求。连接在一段时间不活动之后从游泳池逐出。
由于多处都会使用okhttpClient,所以我把它封装在一个单例类里,便于随处获取
/**
* Created by Newtrek on 2017/4/12.
*/
public class HttpHelper {
public static int DEFAULT_TIMEOUT=5;
private OkHttpClient okHttpClient;
private HttpHelper(){
OkHttpClient.Builder builder=new OkHttpClient.Builder();
// set time out
builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
okHttpClient=builder.build();
}
public static HttpHelper getInstance(){
return HttpHelperHolder.instance;
}
public OkHttpClient getOkHttpClient() {
return okHttpClient;
}
private static class HttpHelperHolder{
private static HttpHelper instance=new HttpHelper();
}
}
/**
* 下载一个文件,并以字符串形式打印响应里的头部,正文
*/
public static void SynchGet(){
// 获取client
OkHttpClient okHttpClient= HttpHelper.getInstance().getOkHttpClient();
// 构造请求,builder源码已分析,不用设置methos就是默认的get方法
Request request=new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
try {
// 获取响应
Response response=okHttpClient.newCall(request).execute();
// 响应成功,就打印相关内容,其实他是判断code是否属于【200,300)
if (response.isSuccessful()){
Headers headers=response.headers();
for (int i=0;i
结果:
public static void AsynchGet(){
// 获取client
OkHttpClient okHttpClient=HttpHelper.getInstance().getOkHttpClient();
// 构造请求
Request request=new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
// 请求加入队列
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
Headers headers=response.headers();
for (int i=0;i
通常,HTTP标头的工作方式与Map
当写请求头时,使用头(名称,值)来设置名称唯一出现的值。
如果存在现有值,则在添加新值之前将删除它们。使用addHeader(name,value)来添加标题,而不会删除已经存在的标题。
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
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();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println("Server: " + response.header("Server"));
System.out.println("Date: " + response.header("Date"));
System.out.println("Vary: " + response.headers("Vary"));
}
使用HTTP POST将请求体发送到服务
public static void postAString() {
MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
String postBody = ""
+ "Releases\n"
+ "--------\n"
+ "\n"
+ " * _1.0_ May 6, 2013\n"
+ " * _1.1_ June 15, 2013\n"
+ " * _1.2_ August 11, 2013\n";
OkHttpClient okHttpClient = HttpHelper.getInstance().getOkHttpClient();
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
.build();
try {
Response response = okHttpClient.newCall(request).execute();
System.out.println( response.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
RequestBody requestBody = new RequestBody() {
@Override public MediaType contentType() {
return MEDIA_TYPE_MARKDOWN;
}
@Override public void writeTo(BufferedSink sink) throws IOException {
sink.writeUtf8("Numbers\n");
sink.writeUtf8("-------\n");
for (int i = 2; i <= 997; i++) {
sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i)));
}
}
private String factor(int n) {
for (int i = 2; i < n; i++) {
int x = n / i;
if (x * i == n) return factor(x) + " × " + i;
}
return Integer.toString(n);
}
};
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
直接 .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file)),file是File类型
RequestBody formBody = new FormBody.Builder()
.add("search", "Jurassic Park")
.build();
MultipartBody.Builder可以构建与HTML文件上传表单兼容的复杂请求体。多部分请求体的每个部分本身就是一个请求体,并且可以定义自己的头。如果存在,这些标题应该描述零件体,例如它的Content-Disposition。如果Content-Length和Content-Type标题可用,则会自动添加。
// Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "Square Logo")
.addFormDataPart("image", "logo-square.png",
RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
.build();
Gist gist = gson.fromJson(response.body().charStream(), Gist.class);
for (Map.Entry entry : gist.files.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue().content);
}
要缓存响应,您需要一个可以读取和写入的缓存目录,并对缓存的大小有限制。缓存目录应该是私有的,不受信任的应用程序不能读取其内容!
响应缓存使用HTTP头进行所有配置,你可以添加请求头,如Cache-Control:max-stale = 3600,OkHttp的缓存将遵守它们。您的网络服务器使用自己的响应头配置响应缓存的时间长短像Cache-Control:max-age = 9600。有缓存标头强制缓存的响应,强制网络响应,或强制使用条件GET验证网络响应
// set cache
int cacheSize=10*1024*1024;//10MB
File cacheFile=new File("E://java/");
if (!cacheFile.exists()){
cacheFile.mkdir();
}
Cache cache=new Cache(cacheFile,cacheSize);
builder.cache(cache);
public static void ResponseCache(){
OkHttpClient okHttpClient = HttpHelper.getInstance().getOkHttpClient();
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
try {
Response response1 = okHttpClient.newCall(request).execute();
String response1Body = response1.body().string();
System.out.println("Response 1 response: " + response1);
System.out.println("Response 1 cache response: " + response1.cacheResponse());
System.out.println("Response 1 network response: " + response1.networkResponse());
} catch (IOException e) {
e.printStackTrace();
}
try {
Response response2 = okHttpClient.newCall(request).execute();
String response2Body = response2.body().string();
System.out.println("Response 2 response: " + response2);
System.out.println("Response 2 cache response: " + response2.cacheResponse());
System.out.println("Response 2 network response: " + response2.networkResponse());
} catch (IOException e) {
e.printStackTrace();
}
}
可以验证到我的E://java/目录里多了四个文件,其实他们就是缓存文件
每次client.newCall()的时候会返回一个Call,所以取消的话就调用call.cancel()就行,如果请求任务未完成,会抛出IO异常
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
OkHttp可以自动重试未经身份验证的请求。当答复为401未授权时,将要求身份验证者提供凭证。实现应该构建一个包含缺少凭据的新请求。如果没有凭据可用,返回null以跳过重试。使用Response.challenges()来获取任何认证挑战的方案和领域。在履行基本挑战时,请使用Credentials.basic(用户名,密码)对请求标头进行编码。
client = new OkHttpClient.Builder()
.authenticator(new Authenticator() {
@Override public Request authenticate(Route route, Response response) throws IOException {
System.out.println("Authenticating for response: " + response);
System.out.println("Challenges: " + response.challenges());
String credential = Credentials.basic("jesse", "password1");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
})
.build();