OkHttp从使用到源代码分析(1)-官方说明

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cj5785/article/details/89228440

目前,Android最火的轻量级网络请求框架莫过于 OkHttp 了,做Android开发的,或多或少都会接触到这个框架,那么这个框架到底怎么用,原理又是什么呢,这边文章的目的就是做一个使用说明和源代码剖析,源代码基于3.14.1

首先,使用任何框架,第一步应该都是去查看官方文档,那么我们就先去官方网站看看官方文档怎么写的吧
传送门:OkHttp

Overview

okhttp_overview

看完预览,注意到几个点,OkHttp是一个HTTP的客户端,支持HTTP/2,使用连接池,支持GZIP,运用缓存等。其中有两句是比较重要的

  • It supports both synchronous blocking calls and async calls with callbacks.
  • OkHttp supports Android 5.0+ (API level 21+) and Java 8+.

第一句说明了OkHttp支持同步和异步调用,同步是阻塞的,异步有回调,第二句则说明了现在版本支持的Android版本和Java版本,Android系统大于等于5.0,且Java版本大于等于8,那么是不是意味着低于5.0或Java 8就不能使用OkHttp了呢,当然不是,在OkHttp的github上可以看到有这样的说明

The OkHttp 3.12.x branch supports Android 2.3+ (API level 9+) and Java 7+. These platforms lack support for TLS 1.2 and should not be used. But because upgrading is difficult we will backport critical fixes to the 3.12.x branch through December 31, 2020.

意思就是OkHttp的3.12.x分支支持Android 2.3+,Java 7+,使用那个分支就可以做到更多的兼容。

Examples

  • GET A URL

This program downloads a URL and prints its contents as a string.

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  try (Response response = client.newCall(request).execute()) {
    return response.body().string();
  }
}
  • POST TO A SERVER

This program posts data to a service.

public static final MediaType JSON
    = MediaType.get("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  try (Response response = client.newCall(request).execute()) {
    return response.body().string();
  }
}

以上两个例子来自官网,可以看到,OkHttp支持get和post两种请求方式,关于get和post的区别,可以参考HTTP|GET 和 POST 区别?网上多数答案都是错的!,这篇文章感觉写到了点子上。

Download

  • v3.14.1 JAR
    You’ll also need Okio, which OkHttp uses for fast I/O and resizable buffers. Download the latest JAR.
    The source code to OkHttp, its samples, and this website is available on GitHub.

  • MAVEN

<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
  <version>3.14.1</version>
</dependency>
  • GRADLE
    implementation 'com.squareup.okhttp3:okhttp:3.14.1'

上面说明了OkHttp的使用方式,第一种,直接下载jar使用,这种方式还必须同时下载Okio,第二种使用MAVEN,第三种,使用GRADLE。

Contributing

在这里插入图片描述

License

Copyright 2016 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

熟悉的Apache License 2.0,嗯~~~

以上便是OkHttp官网首页显示的信息,通过这些我们已经对OkHttp有了一个初步的认识,包括什么是OkHttp,OkHttp的get和post的运用,OkHttp的协议,OkHttp的License信息。那么接下来就进一步探索OkHttp的食用方法,美味餐点即将奉上~

热门开源项目源代码分析导航

猜你喜欢

转载自blog.csdn.net/cj5785/article/details/89228440