基本使用方式
创建Request
Request request = new Request.Builder().url().get().build();
创建OkHttpClient
OkHttpClient client = new OkHttpClient();
public final OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor())
.cache(new Cache(cacheDir, cacheSize))
.build();
OkHttpClient client = client.newBuilder()
.readTimeout(500, TimeUnit.MILLISECONDS)
.build();
执行
Response response = client.newCall(request).execute();
client.newCall(request).enqueue(callback);
清理
client.dispatcher().executorService().shutdown();
client.connectionPool().evictAll();
client.cache().close();
代码流程分析
创建RealCall
private RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
this.client = client;
this.originalRequest = originalRequest;
this.forWebSocket = forWebSocket;
}
static RealCall newRealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
// Safely publish the Call instance to the EventListener.
如果是同步的call
直接调用RealCall.execute
private int maxRequests = 64;
private int maxRequestsPerHost = 5;
@Override public Response execute() throws IOException {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
transmitter.timeoutEnter();
transmitter.callStart();
try {
client.dispatcher().executed(this);
return getResponseWithInterceptorChain();
} finally {
client.dispatcher().finished(this);
}
}
如若调用enqueue方法,就会走到AsyncCall
@Override public void enqueue(Callback responseCallback) {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
transmitter.callStart();
client.dispatcher().enqueue(new AsyncCall(responseCallback));
}
AsyncCall是Runnalbe 包装了callback
client.dispatcher().enqueue调用了promoteAndExecute
private boolean promoteAndExecute() {
assert (!Thread.holdsLock(this));
List<AsyncCall> executableCalls = new ArrayList<>();
boolean isRunning;
synchronized (this) {
for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
AsyncCall asyncCall = i.next();
if (runningAsyncCalls.size() >= maxRequests) break; // Max capacity.
if (asyncCall.callsPerHost().get() >= maxRequestsPerHost) continue; // Host max capacity.
i.remove();
asyncCall.callsPerHost().incrementAndGet();
executableCalls.add(asyncCall);
runningAsyncCalls.add(asyncCall);
}
isRunning = runningCallsCount() > 0;
}
for (int i = 0, size = executableCalls.size(); i < size; i++) {
AsyncCall asyncCall = executableCalls.get(i);
asyncCall.executeOn(executorService());
}
return isRunning;
}
asyncCall.executeOn(executorService());
/**
* Attempt to enqueue this async call on {@code executorService}. This will attempt to clean up
* if the executor has been shut down by reporting the call as failed.
*/
void executeOn(ExecutorService executorService) {
assert (!Thread.holdsLock(client.dispatcher()));
boolean success = false;
try {
executorService.execute(this);
success = true;
} catch (RejectedExecutionException e) {
InterruptedIOException ioException = new InterruptedIOException("executor rejected");
ioException.initCause(e);
transmitter.noMoreExchanges(ioException);
responseCallback.onFailure(RealCall.this, ioException);
} finally {
if (!success) {
client.dispatcher().finished(this); // This call is no longer running!
}
}
}
下面执行到了execute,调用 Response response = getResponseWithInterceptorChain();
@Override protected void execute() {
boolean signalledCallback = false;
transmitter.timeoutEnter();
try {
Response response = getResponseWithInterceptorChain();
signalledCallback = true;
responseCallback.onResponse(RealCall.this, response);
} catch (IOException e) {
if (signalledCallback) {
// Do not signal the callback twice!
Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
} else {
responseCallback.onFailure(RealCall.this, e);
}
} catch (Throwable t) {
cancel();
if (!signalledCallback) {
IOException canceledException = new IOException("canceled due to " + t);
canceledException.addSuppressed(t);
responseCallback.onFailure(RealCall.this, canceledException);
}
throw t;
} finally {
client.dispatcher().finished(this);
}
}
getResponseWithInterceptorChain()
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(client.interceptors());
interceptors.add(new RetryAndFollowUpInterceptor(client));
interceptors.add(new BridgeInterceptor(client.cookieJar()));
interceptors.add(new CacheInterceptor(client.internalCache()));
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
interceptors.addAll(client.networkInterceptors());
}
interceptors.add(new CallServerInterceptor(forWebSocket));
Interceptor.Chain chain = new RealInterceptorChain(interceptors, transmitter, null, 0,
originalRequest, this, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
boolean calledNoMoreExchanges = false;
try {
Response response = chain.proceed(originalRequest);
if (transmitter.isCanceled()) {
closeQuietly(response);
throw new IOException("Canceled");
}
return response;
} catch (IOException e) {
calledNoMoreExchanges = true;
throw transmitter.noMoreExchanges(e);
} finally {
if (!calledNoMoreExchanges) {
transmitter.noMoreExchanges(null);
}
}
}
RealInterceptorChain.proceed
public Response proceed(Request request, Transmitter transmitter, @Nullable Exchange exchange)
throws IOException {
Response response = interceptor.intercept(next);
}
接下来走到Interceptor.intercept
RealCall.getResponseWithInterceptorChain 可以看到有四个
interceptors.add(new RetryAndFollowUpInterceptor(client));
interceptors.add(new BridgeInterceptor(client.cookieJar()));
interceptors.add(new CacheInterceptor(client.internalCache()));
interceptors.add(new ConnectInterceptor(client));
CallServerInterceptor
RetryAndFollowUpInterceptor 处理3XX重定向的,4XX授权,5xx服务器错误retry等
Location
@Override public Response intercept(Chain chain)
Request followUp = followUpRequest(response, route);
case HTTP_MULT_CHOICE:
case HTTP_MOVED_PERM:
case HTTP_MOVED_TEMP:
case HTTP_SEE_OTHER:
// Does the client allow redirects?
if (!client.followRedirects()) return null;
String location = userResponse.header("Location");
BridgeInterceptor
添加了很多header,并且加入了gzip
CacheInterceptor 处理cache
ConnectInterceptor 创建Exechange,建立连接
CallServerInterceptor 调用
exchange.writeRequestHeaders(request);
if (responseBuilder == null) {
responseBuilder = exchange.readResponseHeaders(false);
}
Response response = responseBuilder
.request(request)
.handshake(exchange.connection().handshake())
.sentRequestAtMillis(sentRequestMillis)
.receivedResponseAtMillis(System.currentTimeMillis())
.build();
题外话:
ConnectionPool
最大5个链接,最长 5分钟
实现是RealConnectionPool
Http1.0 很明显无法复用socket链接。
Http2.0 可以复用socket链接
分享到:
相关推荐
赠送jar包:okhttp-3.14.9.jar; 赠送原API文档:okhttp-3.14.9-javadoc.jar; 赠送源代码:okhttp-3.14.9-sources.jar; 赠送Maven依赖信息文件:okhttp-3.14.9.pom; 包含翻译后的API文档:okhttp-3.14.9-javadoc-...
赠送jar包:okhttp-3.14.9.jar; 赠送原API文档:okhttp-3.14.9-javadoc.jar; 赠送源代码:okhttp-3.14.9-sources.jar; 赠送Maven依赖信息文件:okhttp-3.14.9.pom; 包含翻译后的API文档:okhttp-3.14.9-javadoc-...
https请求所需要的资源包
okhttp实现简单封装功能
WebView布局使用,okHTTP访问解析服务器返回的json/xml数据。
okhttp网络通信包,okhttp3jar包是一款可以支持android网络框架之OKhttp的jar包,一个处理网络请求的开源项目,是目前android安卓端最火热的轻量级框架。
分享okhttp-3.14.1.jar下载,官方下载地址:https://square.github.io/okhttp/#download,目前官方已经是最新版本,不提供该版本jar下载。
各版本okhttp+okio 1
本篇文章主要介绍了Android OkHttp完全解析,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
okhttp最新版okhttp-3.9.0.jar下载,网络请求工具类,okio-1.6.0.jar
详见http://blog.csdn.net/huaxun66/article/details/52613439
注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 ...
Android 6.0采用的SPDY支持HTTP上GZIP压缩的传输,这使得OkHttp包的功能能够进一步被利用,本文我们来总结一下Android M(6.0)使用OkHttp包解析和发送JSON请求的教程
OKHttp3源码分析,反编译了OKHttp3项目源码,并对源码整体实现添加了详细的注释,方便查看是想流程。
okhttp请求+FastJson解析数据
当网络出现问题的时候OkHttp依然坚守自己的职责,它会自动恢复一般的连接问题,如果你的服务有多个IP地址,当第一个IP请求失败时,OkHttp会交替尝试你配置的其他IP,OkHttp使用现代TLS技术(SNI, ALPN)初始化新的连接
简单写了个网络请求工具demo,方便以后新项目有网络需求快速搭建网络框架:
okhttp-4.8.1和4.9.1的jar包
赠送jar包:okhttp-2.4.0.jar; 赠送原API文档:okhttp-2.4.0-javadoc.jar; 赠送源代码:okhttp-2.4.0-sources.jar; 赠送Maven依赖信息文件:okhttp-2.4.0.pom; 包含翻译后的API文档:okhttp-2.4.0-javadoc-API...