Android OkHttp

一、OkHttp的介绍

Android系统提供两种HTTP通信类,HttpURLConnection和HttpClient。在Android 2.2版本及其之前的版本使用HttpClient是较好的选择;

而在Android 2.3版本及其之后,HttpURLConnection 则是最佳的选择,它的 API 简单,体积较小,因而非常适用于Android项目。

HttpURLConnection的压缩和缓存机制可以有效地减少网络访问的流量,在提升速度和省电方面也起到了较大的作用。

另外在Android 6.0版本中,HttpClient库被移除了,如果不引用HttpClient,HttpURLConnection则是以后我们唯一的选择.

OkHttp是一个相对成熟的解决方案,Android API19 4.4的源码中可以看到HttpURLConnection已经替换成了okHttp。并且OKhttp支持HTTP/2协议,允许连接到同一个主机地址的所有请求共享Socket,提高请求效率。

在HTTP/2协议不可用的情况下,通过连接池减少请求的延迟。GZip透明压缩减少传输的数据包大小。响应缓存,避免同一个重复的网络请求。

OkHttp的优势:

  • 允许连接到同一个主机地址的所有请求,提高请求效率。
  •  共享Socket,减少对服务器的请求次数。
  •  通过连接池,减少了请求延迟。
  •  缓存响应数据来减少重复的网络请求。
  • 减少了对数据流量的消耗。
  • 自动处理GZip压缩 。

OkHttp的功能:

  • get、post请求
  • 文件的上传下载
  • 加载图片(内部会图片大小自动压缩)
  • 支持请求回调,直接返回对象、对象集合
  • 支持session的保持

二、OkHttp的使用

在模块build.gradle文件dependencies导入OkHttp依赖:

implementation "com.squareup.okhttp3:okhttp:4.2.0"
implementation 'com.squareup.okio:okio:1.12.0'

在AndroidManifest.xml添加网络权限:<uses-permission android:name="android.permission.INTERNET"/>

ResultCallback.java

public abstract class ResultCallback {

 

    public abstract void onError(Request request, Exception e);

 

    public abstract void onResponse(Response response) throws IOException;

 

}

okHttpEngine.java

public class okHttpEngine {

    private static okHttpEngine mInstance;

    private OkHttpClient mOkHttpClient;

    private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown;charset=utf-8");

 

    public static okHttpEngine getInstance(Context mContext){

        if(mInstance == null){

            synchronized (okHttpEngine.class){

                if (mInstance == null){

                    mInstance = new okHttpEngine(mContext);

                }

            }

        }

        return mInstance;

    }

 

    private okHttpEngine(Context mContext){

        File sdcache = mContext.getExternalCacheDir();

        int cacheSize = 2*1024*1024;

        //设置超时时间和缓存

        OkHttpClient.Builder builder = new OkHttpClient.Builder()

                .connectTimeout(15, TimeUnit.SECONDS)

                .writeTimeout(20,TimeUnit.SECONDS)

                .readTimeout(20,TimeUnit.SECONDS)

                .cache(new Cache(sdcache.getAbsoluteFile(),cacheSize));

        mOkHttpClient = builder.build();

    }

 

    /**

     * 异步GET请求

     * @param url

     * @param callback

     */

    public void getAsynHttp(String url,ResultCallback callback){

        final Request request = new Request.Builder().url(url).build();

        Call call = mOkHttpClient.newCall(request);

        dealResult(call,callback);

    }

 

    /**

     * 异步POST请求

     * @param name

     * @param value

     * @param url

     * @param callback

     */

    public void postAsynHttp(String name,String value,String url,ResultCallback callback){

        final RequestBody formBody =  new FormBody.Builder()

                .add(name,value).build();

        final Request request = new Request.Builder()

                .url(url)

                .post(formBody)

                .build();

        Call call = mOkHttpClient.newCall(request);

        dealResult(call,callback);

    }

 

    /**

     * 异步上传文件

     * @param filepath

     * @param url

     * @param callback

     */

    public void uploadAsynHttp(String filepath, String url,ResultCallback callback){

        File file = new File(filepath);

        final Request request = new Request.Builder()

                .url(url)

                .post(RequestBody.create(MEDIA_TYPE_MARKDOWN,file))

                .build();

        Call call = mOkHttpClient.newCall(request);

        dealResult(call,callback);

    }

 

    private void dealResult(Call call, final ResultCallback callback){

        call.enqueue(new Callback() {

            @Override

            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                sendFailedCallback(call.request(),e,callback);

            }

 

            @Override

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

                sendSuccessCallback(response,callback);

            }

 

            private void sendFailedCallback(final Request request,final Exception e,final ResultCallback callback){

                if(callback != null){

                    callback.onError(request,e);

                }

            }

 

            private void sendSuccessCallback(final Response response,final ResultCallback callback){

                if(callback != null){

                    try {

                        callback.onResponse(response);

                    catch (IOException e) {

                        e.printStackTrace();

                    }

                }

            }

        });

    }

 

}

三、OkHttp源码解析

源码下载地址:https://github.com/square/okhttp

OkHttp请求流程:

 

1.Dispatcher任务调度

Dispatcher主要用于控制并发的请求,有两个构造方法,可以使用自己设定的线程池。如果没有设定线程池,则会在请求网络前自己创建默认线程池。

这个线程池类似于 CachedThreadPool,比较适合执行大量的耗时比较少的任务。前面讲过,当调用 RealCall 的 enqueue 方法时,实际上是调用了 Dispatcher的enqueue方法,

 

Dispatcher.java

public final class Dispatcher {

  /**默认最大并发请求数*/

  private int maxRequests = 64;

  /**默认每个主机的最大请求数 */

  private int maxRequestsPerHost = 5;

  /**消费者线程池*/

  private ExecutorService executorService;

  /**将要运行的异步请求队列*/

  private final Deque<AsyncCall> readyCalls = new ArrayDeque<>();

  /**正在运行的异步请求队列*/

  private final Deque<AsyncCall> runningCalls = new ArrayDeque<>();

 

  /**正在运行的同步请求队列 */

  private final Deque<Call> executedCalls = new ArrayDeque<>();

  /**用户自定义线程池,如果不设置则使用默认线程池*/

  public Dispatcher(ExecutorService executorService) {

    this.executorService = executorService;

  }

 

  public Dispatcher() {

  }

 

  public synchronized ExecutorService getExecutorService() {

    if (executorService == null) {

      executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,

          new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher"false));

    }

    return executorService;

  }

  /**当调用 RealCall 的 enqueue 方法时,实际上是调用了 Dispatcher的enqueue方法,

     当正在运行的异步请求队列中的数量小于64并且正在运行的请求主机数小于5时,把请求加载到

     runningAsyncCalls中并在线程池中执行,否则就加入到readyAsyncCalls中进行缓存等待

     参数是AsyncCall,它是Call的内部类,其内部也实现了execute方法,*/

  synchronized void enqueue(AsyncCall call) {

    if (runningCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {

      runningCalls.add(call);

      getExecutorService().execute(call);

    else {

      readyCalls.add(call);

    }

  }

 

2.Interceptor拦截器

拦截器是一种能够监控、重写、重试调用的机制。通常情况下,拦截器用来添加、移除、转换请求和响应的头部信息。

比如将域名替换为IP地址,在请求头中添加host属性;也可以添加我们应用中的一些公共参数,比如设备id、版本号,等等。

在AsyncCall类中execute方法调用getResponseWithInterceptorChain 方法中创建了 ApplicationInterceptorChain,它是一个拦截器链。

这个类也是RealCall的内部类,接下来执行了它的proceed方法每次从拦截器列表中取出拦截器和getResponse方法执行网络请求。

3.缓存策略

HttpEngine.java

//处理网络请求

public void sendRequest() throws RequestException, RouteException, IOException {

  if (cacheStrategy != nullreturn// Already sent.

  if (httpStream != nullthrow new IllegalStateException();

 

  Request request = networkRequest(userRequest);

  /**获取Client中的Cache,同时Cache在初始化中会读取缓存目录中的曾经请求过的所有信息*/

  InternalCache responseCache = Internal.instance.internalCache(client);

  /**获取上次与服务器交互时缓存的Response这里的缓存均基于Map。key是请求中url 的 md5,value 是在文件

     中查询到的缓存,页面置换基于 LRU 算法。*/

  Response cacheCandidate = responseCache != null

      ? responseCache.get(request)

      null;

    

  long now = System.currentTimeMillis();

  /**根据cacheStrategy的处理得到了网络请求networkRequest和缓存响应cacheResponse这两个值*/

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

  networkRequest = cacheStrategy.networkRequest;

  cacheResponse = cacheStrategy.cacheResponse;

  /**根据networkRequest和cacheResponse这两个值的数据是否为null来进行进一步的处理。*/

  if (responseCache != null) {

    responseCache.trackResponse(cacheStrategy);

  }

 

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

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

  }

  /**在networkRequest和cacheResponse都为null的情况下,也就是不进行网络请求并且缓存不存在或者过期,这时返回504错误;

     当networkRequest为null时也就是不进行网络请求,如果缓存可以使用时则直接返回缓存,其他情况则请求网络*/

  if (networkRequest != null) {

    httpStream = connect();

    httpStream.setHttpEngine(this);

    if (callerWritesRequestBody && permitsRequestBody(networkRequest) && requestBodyOut == null) {

      long contentLength = OkHeaders.contentLength(request);

      if (bufferRequestBody) {

        if (contentLength > Integer.MAX_VALUE) {

          throw new IllegalStateException("Use setFixedLengthStreamingMode() or "

              "setChunkedStreamingMode() for requests larger than 2 GiB.");

        }

 

        if (contentLength != -1) {

          // Buffer a request body of a known length.

          httpStream.writeRequestHeaders(networkRequest);

          requestBodyOut = new RetryableSink((int) contentLength);

        else {

          // Buffer a request body of an unknown length. Don't write request

          // headers until the entire body is ready; otherwise we can't set the

          // Content-Length header correctly.

          requestBodyOut = new RetryableSink();

        }

      else {

        httpStream.writeRequestHeaders(networkRequest);

        requestBodyOut = httpStream.createRequestBody(networkRequest, contentLength);

      }

    }

 

  else {

    if (cacheResponse != null) {

      // We have a valid cached response. Promote it to the user response immediately.

      this.userResponse = cacheResponse.newBuilder()

          .request(userRequest)

          .priorResponse(stripBody(priorResponse))

          .cacheResponse(stripBody(cacheResponse))

          .build();

    else {

      // We're forbidden from using the network, and the cache is insufficient.

      this.userResponse = new Response.Builder()

          .request(userRequest)

          .priorResponse(stripBody(priorResponse))

          .protocol(Protocol.HTTP_1_1)

          .code(504)

          .message("Unsatisfiable Request (only-if-cached)")

          .body(EMPTY_BODY)

          .build();

    }

 

    userResponse = unzip(userResponse);

  }

}

HttpEngine.java

//解析HTTP响应报头,如果有缓存并且可用,则用缓存的数据并更新缓存,否则就用网络请求返回的数据。

public void readResponse() throws IOException {

  if (userResponse != null) {

    return// Already ready.

  }

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

    throw new IllegalStateException("call sendRequest() first!");

  }

  if (networkRequest == null) {

    return// No network response to read.

  }

 

  Response networkResponse;

  /**读取网络响应*/

  if (forWebSocket) {

    httpStream.writeRequestHeaders(networkRequest);

    networkResponse = readNetworkResponse();

 

  else if (!callerWritesRequestBody) {

    networkResponse = new NetworkInterceptorChain(0, networkRequest).proceed(networkRequest);

 

  else {

    // Emit the request body's buffer so that everything is in requestBodyOut.

    if (bufferedRequestBody != null && bufferedRequestBody.buffer().size() > 0) {

      bufferedRequestBody.emit();

    }

 

    // Emit the request headers if we haven't yet. We might have just learned the Content-Length.

    if (sentRequestMillis == -1) {

      if (OkHeaders.contentLength(networkRequest) == -1

          && requestBodyOut instanceof RetryableSink) {

        long contentLength = ((RetryableSink) requestBodyOut).contentLength();

        networkRequest = networkRequest.newBuilder()

            .header("Content-Length", Long.toString(contentLength))

            .build();

      }

      httpStream.writeRequestHeaders(networkRequest);

    }

 

    // Write the request body to the socket.

    if (requestBodyOut != null) {

      if (bufferedRequestBody != null) {

        // This also closes the wrapped requestBodyOut.

        bufferedRequestBody.close();

      else {

        requestBodyOut.close();

      }

      if (requestBodyOut instanceof RetryableSink) {

        httpStream.writeRequestBody((RetryableSink) requestBodyOut);

      }

    }

 

    networkResponse = readNetworkResponse();

  }

 

  receiveHeaders(networkResponse.headers());

 

  /**检查缓存是否可用,如果可以用就是要当前缓存的response,关闭网络连接,释放连接*/

  if (cacheResponse != null) {

    if (validate(cacheResponse, networkResponse)) {

      userResponse = cacheResponse.newBuilder()

          .request(userRequest)

          .priorResponse(stripBody(priorResponse))

          .headers(combine(cacheResponse.headers(), networkResponse.headers()))

          .cacheResponse(stripBody(cacheResponse))

          .networkResponse(stripBody(networkResponse))

          .build();

      networkResponse.body().close();

      releaseStreamAllocation();

 

      // Update the cache after combining headers but before stripping the

      // Content-Encoding header (as performed by initContentStream()).

      InternalCache responseCache = Internal.instance.internalCache(client);

      responseCache.trackConditionalCacheHit();

      responseCache.update(cacheResponse, stripBody(userResponse));

      userResponse = unzip(userResponse);

      return;

    else {

      closeQuietly(cacheResponse.body());

    }

  }

 

  userResponse = networkResponse.newBuilder()

      .request(userRequest)

      .priorResponse(stripBody(priorResponse))

      .cacheResponse(stripBody(cacheResponse))

      .networkResponse(stripBody(networkResponse))

      .build();

 

  if (hasBody(userResponse)) {

    maybeCache();

    userResponse = unzip(cacheWritingResponse(storeRequest, userResponse));

  }

}

4.失败重连

在Call的getResponse方法中如果发生 IOException 或者 RouteException 时都会执行HttpEngine的recover方法

HttpEngine.java

public HttpEngine recover(RouteException e) {

  if (!streamAllocation.recover(e)) {

    return null;

  }

 

  if (!client.getRetryOnConnectionFailure()) {

    return null;

  }

 

  StreamAllocation streamAllocation = close();

 

  // 重新创建HttpEngine并返回,用来完成重连。

  return new HttpEngine(client, userRequest, bufferRequestBody, callerWritesRequestBody,

      forWebSocket, streamAllocation, (RetryableSink) requestBodyOut, priorResponse);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值