okhttp enqueue流程,android开发流程框架

本文详细介绍了OkHttp的异步请求执行流程,从检查最大并发限制到将请求加入线程池执行,再到异常处理和回调机制,深入理解Android开发中的网络请求框架。
摘要由CSDN通过智能技术生成

synchronized (this) {
for (Iterator i = readyAsyncCalls.iterator(); i.hasNext(); ) {
AsyncCall asyncCall = i.next();

if (runningAsyncCalls.size() >= maxRequests) break; // Max capacity.
最大同时运行数请求为64个。超过就退出循环
if (asyncCall.callsPerHost().get() >= maxRequestsPerHost) continue; // Host max capacity.
每个服务器最大运行请求数位为5 个。超过就结束接下来操作
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());每个异步请求被封装成asyncCall

final class AsyncCall extends NamedRunnable {

}
public abstract class NamedRunnable implements Runnable {
protected final String name;

public NamedRunnable(String format, Object… args) {
this.name = Util.format(format, args);
}

@Override public final void run() {
String oldName = Thread.currentThread().getName();
Thread.currentThread().setName(name);
try {
run方法中执行execute,
execute();
} finally {
Thread.currentThread().setName(oldName);
}
}

protected abstract void execute();
}

void executeOn(ExecutorService executorService) {
assert (!Thread.holdsLock(client.dispatcher()));
boolean success = false;
try {
executorService.execute(this);
此处会在线程池中执行AsyncCall 的run方法,即执行 AsyncCall 的execute方法 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!
}
}
}

@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;
dSuppressed(t);
//失败回调
responseCallback.onFailure(RealCall.this, canceledException);
}
throw t;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值