转载 Okhttp3基本使用 基本使用——OkHttp3详细使用教程

一、简介

HTTP是现代应用常用的一种交换数据和媒体的网络方式,高效地使用HTTP能让资源加载更快,节省带宽。

OkHttp是一个高效的HTTP客户端,它有以下默认特性:

  • 支持HTTP/2,允许所有同一个主机地址的请求共享同一个socket连接
  • 连接池减少,请求连接复用以提高效率
  • 透明的GZIP压缩减少响应数据的大小
  • 缓存响应内容,避免一些完全重复的请求
  • 当网络出现问题时,OkHttp 会自动重试一个主机的多个 IP 地址

  当网络出现问题的时候OkHttp依然坚守自己的职责,它会自动恢复一般的连接问题,如果你的服务有多个IP地址,当第一个IP请求失败时,OkHttp会交替尝试你配置的其他IP,OkHttp使用现代TLS技术(SNI, ALPN)初始化新的连接,当握手失败时会回退到TLS 1.0。

注:

  • OkHttp 支持 Android 2.3 及以上版本Android平台, 对于 Java, JDK 1.7及以上.。
  • socket:网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。

二、使用例子:

讲解:

Requests-请求:
  每一个HTTP请求中都应该包含一个URL,一个GET或POST方法以及Header或其他参数,当然还可以含特定内容类型的数据流。request一旦build()之后,便不可修改。

Responses-响应:
  响应则包含一个回复代码(200代表成功,404代表未找到),Header和定制可选的body。

OkHttpClient:
  OkHttpClient可以发送一个Http请求,并读取该Http请求的响应,它是一个生产Call的工厂。 此外,受益于一个共享的响应缓存/线程池/复用的连接等因素,绝大多数应用使用一个OkHttpClient实例,便可以满足整个应用的Http请求。

三种创建实例的方法:

  • 创建一个默认配置OkHttpClient,可以使用默认的构造函数。
  • 通过new OkHttpClient.Builder()方法来一步一步配置一个OkHttpClient实例。
  • 如果要求使用现有的实例,可以通过newBuilder()方法来进行构造。
OkHttpClient client = new OkHttpClient();
OkHttpClient clientWith30sTimeout = client.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.build();
OkHttpClient client = client.newBuilder().build();

1.get请求方式

1.1 get异步提交请求

/**
* get异步提交请求
* @param url
*/
public static void getAsyn(String url) {
// 1.创建OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
// 2.创建Request对象,设置一个url地址,设置请求方式。默认为get请求,可以不写。
final Request request = new Request.Builder()
.url(url)
.get()
.build();
// 3.创建一个call对象,参数就是Request请求对象
Call call = okHttpClient.newCall(request);
// 4.请求加入调度,重写回调方法
call.enqueue(new Callback() {
// 请求失败执行的方法
@Override
public void onFailure(Call call, IOException e) {
logger.info("getAsyn-call"+call);
logger.info("getAsyn-IOException:"+e);
}
// 请求成功执行的方法
@Override
public void onResponse(Call call, Response response) throws IOException {
logger.info("getAsyn-response:"+response);
System.out.println(response.body().string());
System.out.println(response.body().byteStream());
logger.info("call"+call);
}
});
}

1.2 get同步提交请求

/**
* get同步提交请求
* @param url
*/
public static void getSyn(String url) {
// 1.创建OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
// 2.创建Request对象,设置一个url地址,设置请求方式。
final Request request = new Request.Builder().url(url).build();
// 3.创建一个call对象,参数就是Request请求对象
final Call call = okHttpClient.newCall(request);
// 4.同步调用会阻塞主线程,这边在子线程进行
new Thread(new Runnable() {
@Override
public void run() {
try {
// 同步调用,返回Response,会抛出IO异常
Response response = call.execute();
logger.info("getSyn-response:"+response);
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}

1.3 get异步下载文件

/**
* get异步下载文件
* @param url
*/
public static void getFile(String url) {
// 1.创建OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
// 2.创建Request对象,设置一个url地址,设置请求方式。
Request request = new Request.Builder()
.url(url)
.get()
.build();
// 3.创建一个call对象,参数就是Request请求对象,重写回调方法。
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
logger.info("getFile-call"+call);
logger.info("getFile-IOException:"+e);
} @Override
public void onResponse(Call call, Response response) throws IOException {
// 拿到字节流
InputStream is = response.body().byteStream();
int len = 0;
// 设置下载图片存储路径和名称
File file = new File("F:/img/","baidu.png"); FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[128];
while ((len = is.read(buf)) != -1) {
fos.write(buf,0,len);
}
fos.flush();
fos.close();
is.close();
}
});
}

2.post请求方式

2.1 post异步请求String

/**
* post异步请求String
* @param url
*/
public static void postString(String url) {
// "类型,字节码"
MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
// 1.创建OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
String requestBody = "{name:nana}";
// 3.创建Request对象,设置URL地址,将RequestBody作为post方法的参数传入
Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(mediaType,requestBody))
.build(); // 4.创建一个call对象,参数就是Request请求对象。请求加入调度,重写回调方法
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
logger.info("postString-call"+call);
logger.info("postString-IOException:"+e);
} @Override
public void onResponse(Call call, Response response) throws IOException {
logger.info("postString-response:"+response);
System.out.println(response.body().string());
System.out.println(response.body().byteStream());
logger.info("postString-call"+call);
}
});
}

2.2 post异步请求流

/**
* post异步请求流
* @param url
*/
public static void postStreaming(String url) {
final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown;charset=utf-8");
File file = new File("F:/img/baidu.png");
try {
FileInputStream fileInputStream = new FileInputStream(file); RequestBody requestBody = new RequestBody() {
@Nullable
@Override
public MediaType contentType() {
return MEDIA_TYPE_MARKDOWN;
} @Override
public void writeTo(BufferedSink bufferedSink) throws IOException {
OutputStream outputStream = bufferedSink.outputStream();
int length = 0;
byte[] buffer = new byte[1024];
while ((length = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer,0,length);
}
}
};
Request request = new Request.Builder().
url(url).
post(requestBody).
build();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newCall(request).enqueue(new Callback() { @Override
public void onFailure(Call call, IOException e) {
logger.info("postFlow-call"+call);
logger.info("postFlow-IOException:"+e);
} @Override
public void onResponse(Call call, Response response) throws IOException {
logger.info("postFlow-response:"+response);
logger.info(response.protocol()+" "+response.code() + " " + response.message());
Headers headers = response.headers();
for (int i=0;i< headers.size();i++) {
System.out.println(headers.name(i)+":"+headers.value(i));
}
System.out.println(response.body().string());
logger.info("postFlow-call"+call);
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
} }

2.3 post异步上传Multipart文件

/**
* post异步上传Multipart文件
* @param url
*/
public static void postMultipart(String url) {
OkHttpClient okHttpClient = new OkHttpClient();
File file = new File("F:/img/","baidu.png");
MediaType mediaType = MediaType.parse("image/png");
RequestBody requestBody = new MultipartBody.Builder()
// 设置表单类型
.setType(MultipartBody.FORM)
// 添加数据
.addFormDataPart("name","nana")
.addFormDataPart("file","baidu.png",RequestBody.create(mediaType,file))
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
logger.info("postFlow-call"+call);
logger.info("postFlow-IOException:"+e);
} @Override
public void onResponse(Call call, Response response) throws IOException {
logger.info("postMultiportBody-response:"+response);
System.out.println(response.body().string());
logger.info("postMultiportBody-call"+call);
}
});
}

2.4 post异步请求文件

/**
* post异步请求文件
* @param url
*/
public static void postFile(String url) {
MediaType mediaType = MediaType.parse("text/x-markdown;charset=utf-8");
OkHttpClient okHttpClient = new OkHttpClient();
File file = new File("F:/img/baidu.png");
Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(mediaType,file))
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
logger.info("postFile-call"+call);
logger.info("postFile-IOException:"+e);
} @Override
public void onResponse(Call call, Response response) throws IOException {
logger.info("postFile-response:"+response);
logger.info(response.protocol()+" "+response.code() + " " + response.message());
Headers headers = response.headers();
for (int i=0;i< headers.size();i++) {
System.out.println(headers.name(i)+":"+headers.value(i));
}
System.out.println(response.body().string());
System.out.println(response.body().byteStream());
logger.info("postFile-call"+call);
}
});
}

2.5 post异步请求表单

/**
* post异步请求表单
* @param url
*/
public static void postForm(String url) {
// 1.创建OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
// 2.通过new FormBody()调用build方法,创建一个RequestBody,可以用add添加键值对
RequestBody requestBody = new FormBody.Builder()
.add("name","nana")
.build();
// 3.创建Request对象,设置URL地址,将RequestBody作为post方法的参数传入
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
// 4.创建一个call对象,参数就是Request请求对象。请求加入调度,重写回调方法
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if(e.toString().contains("closed")) {
// 主动取消的请情况下
}
logger.info("postForm-call"+call);
logger.info("postForm-IOException:"+e);
} @Override
public void onResponse(Call call, Response response) throws IOException {
logger.info("postForm-response:"+response);
logger.info(response.protocol()+" "+response.code() + " " + response.message());
Headers headers = response.headers();
for (int i=0;i< headers.size();i++) {
System.out.println(headers.name(i)+":"+headers.value(i));
}
System.out.println(response.body().string());
System.out.println(response.body().byteStream());
logger.info("postForm-call"+call);
}
});
}

2.6 post异步请求表单

/**
* post分块请求
* @param url
*/
public static void postMultiportBody(String url) {
final String IMGUR_CLIENT_ID = "...";
final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
OkHttpClient client = new OkHttpClient();
MultipartBody body = new MultipartBody.Builder("Axad")
.setType(MultipartBody.FORM)
.addPart(
Headers.of("Content-Disposition","form-data;name='name'"),
RequestBody.create(null,"nana")
)
.addPart(
Headers.of("Content-Dispostion","form-data;name='image'"),
RequestBody.create(MEDIA_TYPE_PNG,new File("img.png"))
)
.build();
Request request = new Request.Builder()
.header("Authorization","Client-ID "+IMGUR_CLIENT_ID)
.url(url)
.build(); Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
logger.info("postMultiportBody-call"+call);
logger.info("postMultiportBody-IOException:"+e);
} @Override
public void onResponse(Call call, Response response) throws IOException {
logger.info("postMultiportBody-response:"+response);
System.out.println(response.body().string());
logger.info("postMultiportBody-call"+call);
}
});
}

2.7 post拦截器

/**
* post拦截器
* @param url
*/
public static void postInterceptor(String url) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggerInterceptor())
.build();
Request request = new Request.Builder()
.url(url)
.header("User-Agent","OKHttp Example")
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
logger.info("postInterceptor-call"+call);
logger.info("postInterceptor-IOException:"+e);
} @Override
public void onResponse(Call call, Response response) throws IOException {
logger.info("postInterceptor-response:"+response);
ResponseBody body = response.body();
if (body != null) {
System.out.println(response.body().string());
body.close();
}
logger.info("postInterceptor-call:"+call);
}
});
}
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.IOException; public class LoggerInterceptor implements Interceptor {
private static final Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);
private static final String TAG = "LoggingInterceptor";
@Override
public Response intercept(Chain chain) throws IOException {
// 拦截请求,获取到该次请求的request
Request request = chain.request(); long startTime = System.nanoTime();
logger.info(String.format("Sending request %s on %s%n%s",
request.url(),chain.connection(),request.headers()));
// 执行本次网络请求操作返回response
Response response = chain.proceed(request);
long endTime = System.nanoTime();
logger.info(String.format("Received response for %s in %.1fms%n%s",
response.request().url(),(endTime - startTime)/1e6d,response.headers()));
return response;
}
}

OKHttp3学习的更多相关文章

  1. Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava

    Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava Retrofit,因为其简单与出色的性能,也是受到很多人的青睐,但是他和以往的通信 ...

  2. JAVA学习笔记 (okHttp3的用法)

    最近的项目中有个接口是返回文件流数据,根据我们这边一个验签的插件,我发现里面有okHttpClient提供了Call.Factory,所以就学习了下okHttp3的用法. 1.概述 okhttp是一个 ...

  3. Android 框架学习之 第一天 okhttp & Retrofit

    最近面试,一直被问道新技术新框架,这块是短板,慢慢补吧. 关于框架的学习,分几个步骤 I.框架的使用 II.框架主流使用的版本和Android对应的版本 III.框架的衍生使用比如okhttp就会有R ...

  4. 【Android】OkHttp3总结与封装

    开始使用 在app目录下的build.gradle中添加依赖: implementation 'com.squareup.okhttp3:okhttp:3.13.1' implementation ' ...

  5. Android 技能图谱学习路线

    这里是在网上找到的一片Android学习路线,希望记录下来供以后学习 1Java 基础 Java Object类方法 HashMap原理,Hash冲突,并发集合,线程安全集合及实现原理 HashMap ...

  6. Android开发技术周报176学习记录

    Android开发技术周报176学习记录 教程 当 OkHttp 遇上 Http 2.0 http://fucknmb.com/2018/04/16/%E5%BD%93OkHttp%E9%81%87% ...

  7. springcloud Ribbon学习笔记二

    之前介绍了如何搭建eureka服务并开发了一个用户服务成功注册到了eureka中,接下来介绍如何通过ribbon来从eureka中获取用户服务: springcloud ribbon提供客户端的负载均 ...

  8. Android学习之基础知识十二 — 第二讲:网络编程的最佳实践

    上一讲已经掌握了HttpURLConnection和OkHttp的用法,知道如何发起HTTP请求,以及解析服务器返回的数据,但是也许你还没发现,之前我们的写法其实是很有问题的,因为一个应用程序很可能会 ...

  9. android-------- 常用且应该学习的框架

    今天来分享一下一些常用的库,在Github 上 star数也是很高的,开发中也是很常用的: 简单的分享一下,一起学习. http://www.xiufm.com/blog-1-944.html 框架名 ...

随机推荐

  1. centos关闭selinux

    SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.在这种访问控制体系的限制下,进程只能访问那 ...

  2. varchar(n)跟varchar(max)的区别

    本文来源于翁舒航的博客,点击即可跳转原文观看!!!(被转载或者拷贝走的内容可能缺失图片.视频等原文的内容) 若网站将链接屏蔽,可直接拷贝原文链接到地址栏跳转观看,原文链接:https://www.cn ...

  3. SuperSubScriptHelper——Unicode上下标辅助类

    在项目的实施过程中,类似化学分子式.平方.立方等,需要处理上.下标字符. 上下标字符的实现,大致有两种方式,一种是字符本身包含上下标信息,另一种方式是通过格式化标记实现上下标字符的显示. Word中的 ...

  4. jQuery 滚动条滚动

    1.将div的滚动条滚动到最底端 <div class="container"></div> var $container=$(".contain ...

  5. Activiti 数据库表自动生成策略

    Activiti 引擎启动时默认会检测数据库版本与程序版本是否相符,不相符就会抛出异常停止引擎的初始化. 这一策略可以通过引擎的初始化配置参数databaseSchemaUpdate来控制, 如下图的 ...

  6. html5 移动端开发

    移动端开发总结     目录 1.手机与浏览器 2.Viewport(视窗) 3. 媒体查询 4.px,em,rem,pt 5.设备像素比devicePixelRatio 6.移动web中的图标及字体 ...

  7. MUI框架-12-使用原生底部选项卡(凸出图标案例)

    MUI框架-12-使用原生底部选项卡(凸出图标案例) 今天,用 mui 做 app 时,遇到了可能各位都遇到过的头疼问题:底部中间图标凸起,如下图: 最后有源代码 [提示]:有人问我在 HBuilde ...

  8. 我的MBTI小测试

    今天做了自己的MBTI测试,选了93道题版本的,测试结果是ESFP表演者型——有我在就有笑声.这个测试很有趣,我也觉得很神奇. 一.我的MBTI图形 二.才储分析:我的性格类型倾向为“ ESFP ”( ...

  9. 使用Virtual Audio Cable软件实现电脑混音支持电脑录音

    http://blog.csdn.net/cuoban/article/details/50552644

  10. winform打包发布安装包详解..

    winform打包发布安装包详解..   使用VS 自带的打包工具,制作winform安装项目 开发环境:VS 2008 Access 操作系统:Windows XP 开发语言:C# 项目名称:**管 ...