Android OkHttp使用和源码详解(1)

.url(url)

.build();

builder.build().newCall(request).enqueue(new Callback() {

@Override

public void onFailure(Call call, IOException e) {}

@Override

public void onResponse(Call call, Response response) throws IOException {}

});

}

请求操作的起点从 OkHttpClient.newCall().enqueue() 方法开始

OkHttpClient.newCall


@Override public Call newCall(Request request) {

return RealCall.newRealCall(this, request, false /* for web socket */);

}

RealCall.newRealCall.java

static RealCall newRealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {

// Safely publish the Call instance to the EventListener.

RealCall call = new RealCall(client, originalRequest, forWebSocket);

call.transmitter = new Transmitter(client, call);

return call;

}

这个方法会返回一个 RealCall 对象,通过它将网络请求操作添加到请求队列中。

RealCall.enqueue


@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));

}

client.dispatcher()返回Dispatcher,调用 Dispatcher 的 enqueue 方法,执行一个异步网络请求的操作。

Dispatcher 是 OkHttpClient 的调度器,是一种门户模式。主要用来实现执行、取消异步请求操作。本质上是内部维护了一个线程池去执行异步操作,并且在 Dispatcher 内部根据一定的策略,保证最大并发个数、同一 host 主机允许执行请求的线程个数等。

Dispatcher.enqueue


void enqueue(AsyncCall call) {

synchronized (this) {

readyAsyncCalls.add(call);

if (!call.get().forWebSocket) {

AsyncCall existingCall = findExistingCallWithHost(call.host());

if (existingCall != null) call.reuseCallsPerHostFrom(existingCall);

}

}

promoteAndExecute();

}

实际上就是使用线程池执行了一个 AsyncCall,而 AsyncCall 继承了 NamedRunnable,NamedRunnable 实现了 Runnable 接口,因此整个操作会在一个子线程(非 UI 线程)中执行。

NamedRunnable


/**

* Runnable implementation which always sets its thread name.

*/

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 {

execute();

} finally {

Thread.currentThread().setName(oldName);

}

}

protected abstract void execute();

}

在 run 方法中执行了 一个抽象方法 execute 这个抽象方法被 AsyncCall 实现。

AsyncCall.execute


@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);

}

} finally {

client.dispatcher().finished(this);

}

}

从上面看出而真正获取请求结果的方法是在 getResponseWithInterceptorChain 方法中,从名字也能看出其内部是一个拦截器的调用链。

RealCall.getResponseWithInterceptorChain


Response getResponseWithInterceptorChain() throws IOException {

// Build a full stack of interceptors.

List 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);

}

}

}

Interceptor:拦截器是一种强大的机制,可以监视、重写和重试调用。

每一个拦截器的作用如下:

  • BridgeInterceptor:主要对 Request 中的 Head 设置默认值,比如 Content-Type、Keep-Alive、Cookie 等。

  • CacheInterceptor:负责 HTTP 请求的缓存处理。

  • ConnectInterceptor:负责建立与服务器地址之间的连接,也就是 TCP 链接。

  • CallServerInterceptor:负责向服务器发送请求,并从服务器拿到远端数据结果。

  • RetryAndFollowUpInterceptor:此拦截器从故障中恢复,并根据需要执行重定向。如果呼叫被取消,它可能会引发IOException。

在添加上述几个拦截器之前,会调用 client.interceptors 将开发人员设置的拦截器添加到列表当中。

对于 Request 的 Head 以及 TCP 链接,我们能控制修改的成分不是很多。所以咱们了解 CacheInterceptorCallServerInterceptor

CacheInterceptor 缓存拦截器

======================

CacheInterceptor 主要做以下几件事情:

1、根据 Request 获取当前已有缓存的 Response(有可能为 null),并根据获取到的缓存 Response,创建 CacheStrategy 对象。

2、 通过 CacheStrategy 判断当前缓存中的 Response 是否有效(比如是否过期),如果缓存 Response 可用则直接返回,否则调用 chain.proceed() 继续执行下一个拦截器,也就是发送网络请求从服务器获取远端 Response。

3、如果从服务器端成功获取 Response,再判断是否将此 Response 进行缓存操作。

CacheInterceptor.intercept


@Override public Response intercept(Chain chain) throws IOException {

//根据 Request 获取当前已有缓存的 Response(有可能为 null),并根据获取到的缓存 Response

Response cacheCandidate = cache != null

? cache.get(chain.request())

: null;

//获取当前时间

long now = System.currentTimeMillis();

//创建 CacheStrategy 对象

//通过 CacheStrategy 来判断缓存是否有效

CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();

Request networkRequest = strategy.networkRequest;

Response cacheResponse = strategy.cacheResponse;

if (cache != null) {

cache.trackResponse(strategy);

}

if (cacheCandidate != null && cacheResponse == null) {

closeQuietly(cacheCandidate.body()); // The cache candidate wasn’t applicable. Close it.

}

//如果我们被禁止使用网络,并且缓存不足,则失败。返回空相应(Util.EMPTY_RESPONSE)

if (networkRequest == null && cacheResponse == null) {

return new Response.Builder()

.request(chain.request())

.protocol(Protocol.HTTP_1_1)

.code(504)

.message(“Unsatisfiable Request (only-if-cached)”)

.body(Util.EMPTY_RESPONSE)

.sentRequestAtMillis(-1L)

.receivedResponseAtMillis(System.currentTimeMillis())

.build();

}

// 如果缓存有效,缓存 Response 可用则直接返回

if (networkRequest == null) {

return cacheResponse.newBuilder()

.cacheResponse(stripBody(cacheResponse))

.build();

}

//没有缓存或者缓存失败,则发送网络请求从服务器获取Response

Response networkResponse = null;

try {

//执行下一个拦截器,networkRequest

//发起网络请求

networkResponse = chain.proceed(networkRequest);

} finally {

//如果我们在I/O或其他方面崩溃,请不要泄漏cache body。

if (networkResponse == null && cacheCandidate != null) {

closeQuietly(cacheCandidate.body());

}

}

。。。

//通过网络获取最新的Response

Response response = networkResponse.newBuilder()

.cacheResponse(stripBody(cacheResponse))

.networkResponse(stripBody(networkResponse))

.build();

//如果开发人员有设置自定义cache,则将最新response缓存

if (cache != null) {

if (HttpHeaders.hasBody(response) && CacheStrategy.isCacheable(response, networkRequest)) {

// Offer this request to the cache.

CacheRequest cacheRequest = cache.put(response);

return cacheWritingResponse(cacheRequest, response);

}

//返回response(缓存或网络)

return response;

}

通过 Cache 实现缓存功能


通过上面缓存拦截器的流程可以看出,OkHttp 只是规范了一套缓存策略,但是具体使用何种方式将数据缓存到本地,以及如何从本地缓存中取出数据,都是由开发人员自己定义并实现,并通过 OkHttpClient.Builder 的 cache 方法设置。

OkHttp 提供了一个默认的缓存类 Cache.java,我们可以在构建 OkHttpClient 时,直接使用 Cache 来实现缓存功能。只需要指定缓存的路径,以及最大可用空间即可,如下所示:

OkHttpClient.Builder builder = new OkHttpClient.Builder();

builder.connectTimeout(15, TimeUnit.SECONDS)//设置超时

拦截器

.addInterceptor(new Interceptor() {

@Override

public Response intercept(Chain chain) throws IOException {

return null;

}

})

//设置代理

.proxy(new Proxy(Proxy.Type.HTTP,null))

//设置缓存

//AppGlobalUtils.getApplication() 通过反射得到Application实例

//getCacheDir内置 cache 目录作为缓存路径

//maxSize 1010241024 设置最大缓存10MB

.cache(new Cache(AppGlobalUtils.getApplication().getCacheDir(),

1010241024));

Cache 内部使用了 DiskLruCach 来实现具体的缓存功能,如下所示:

/**

* Create a cache of at most {@code maxSize} bytes in {@code directory}.

*/

public Cache(File directory, long maxSize) {

this(directory, maxSize, FileSystem.SYSTEM);

}

Cache(File directory, long maxSize, FileSystem fileSystem) {

this.cache = DiskLruCache.create(fileSystem, directory, VERSION, ENTRY_COUNT, maxSize);

}

DiskLruCache 最终会将需要缓存的数据保存在本地。如果感觉 OkHttp 自带的这套缓存策略太过复杂,我们可以设置使用 DiskLruCache 自己实现缓存机制。

LRU:是近期最少使用的算法(缓存淘汰算法),它的核心思想是当缓存满时,会优先淘汰那些近期最少使用的缓存对象。采用LRU算法的缓存有两种:LrhCache和DisLruCache,分别用于实现内存缓存和硬盘缓存,其核心思想都是LRU缓存算法。

CallServerInterceptor 详解

========================

CallServerInterceptor 是 OkHttp 中最后一个拦截器,也是 OkHttp 中最核心的网路请求部分。

CallServerInterceptor.intercept


@Override public Response intercept(Chain chain) throws IOException {

//获取RealInterceptorChain

RealInterceptorChain realChain = (RealInterceptorChain) chain;

//获取Exchange

Exchange exchange = realChain.exchange();

Request request = realChain.request();

long sentRequestMillis = System.currentTimeMillis();

exchange.writeRequestHeaders(request);

boolean responseHeadersStarted = false;

Response.Builder responseBuilder = null;

if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {

if (“100-continue”.equalsIgnoreCase(request.header(“Expect”))) {

exchange.flushRequest();

responseHeadersStarted = true;

exchange.responseHeadersStart();

responseBuilder = exchange.readResponseHeaders(true);

}

if (responseBuilder == null) {

if (request.body().isDuplex()) {

exchange.flushRequest();

BufferedSink bufferedRequestBody = Okio.buffer(

exchange.createRequestBody(request, true));

request.body().writeTo(bufferedRequestBody);

} else {

// Write the request body if the “Expect: 100-continue” expectation was met.

BufferedSink bufferedRequestBody = Okio.buffer(

exchange.createRequestBody(request, false));

request.body().writeTo(bufferedRequestBody);

bufferedRequestBody.close();

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

结语

看到这篇文章的人不知道有多少是和我一样的Android程序员。

35岁,这是我们这个行业普遍的失业高发阶段,这种情况下如果还不提升自己的技能,进阶发展,我想,很可能就是本行业的职业生涯的终点了。

我们要有危机意识,切莫等到一切都成定局时才开始追悔莫及。只要有规划的,有系统地学习,进阶提升自己并不难,给自己多充一点电,你才能走的更远。

千里之行始于足下。这是上小学时,那种一元钱一个的日记本上每一页下面都印刷有的一句话,当时只觉得这句话很短,后来渐渐长大才慢慢明白这句话的真正的含义。

有了学习的想法就赶快行动起来吧,不要被其他的事情牵绊住了前行的脚步。不要等到裁员时才开始担忧,不要等到面试前一晚才开始紧张,不要等到35岁甚至更晚才开始想起来要学习要进阶。

给大家一份系统的Android学习进阶资料,希望这份资料可以给大家提供帮助。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!**

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

[外链图片转存中…(img-eANB1L1j-1713721785145)]

结语

看到这篇文章的人不知道有多少是和我一样的Android程序员。

35岁,这是我们这个行业普遍的失业高发阶段,这种情况下如果还不提升自己的技能,进阶发展,我想,很可能就是本行业的职业生涯的终点了。

我们要有危机意识,切莫等到一切都成定局时才开始追悔莫及。只要有规划的,有系统地学习,进阶提升自己并不难,给自己多充一点电,你才能走的更远。

千里之行始于足下。这是上小学时,那种一元钱一个的日记本上每一页下面都印刷有的一句话,当时只觉得这句话很短,后来渐渐长大才慢慢明白这句话的真正的含义。

有了学习的想法就赶快行动起来吧,不要被其他的事情牵绊住了前行的脚步。不要等到裁员时才开始担忧,不要等到面试前一晚才开始紧张,不要等到35岁甚至更晚才开始想起来要学习要进阶。

给大家一份系统的Android学习进阶资料,希望这份资料可以给大家提供帮助。
[外链图片转存中…(img-l7j65Jrf-1713721785145)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 8
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值