简介
HTTP是现代应用常用的一种交换数据和媒体的网络方式,高效地使用HTTP能让资源加载更快,节省带宽。OkHttp是一个高效的HTTP客户端,它有以下默认特性:
- 支持HTTP/2,允许所有同一个主机地址的请求共享同一个socket连接
- 连接池减少请求延时
- 透明的GZIP压缩减少响应数据的大小
- 缓存响应内容,避免一些完全重复的请求
源码:https://github.com/square/okhttp
说明:OkHttp支持Android 2.3及以上版本Android平台,对于Java, JDK1.7及以上。
当网络出现问题的时候OkHttp依然坚守自己的职责,它会自动恢复一般的连接问题,如果你的服务有多个IP地址,当第一个IP请求失败时,OkHttp会交替尝试你配置的其他IP,OkHttp使用现代TLS技术(SNI, ALPN)初始化新的连接,当握手失败时会回退到TLS 1.0。
简单使用
引入maven依赖
com.squareup.okhttp3
okhttp
4.0.0-RC1
请求方法
同步请求
就是执行请求的操作是阻塞式,直到 HTTP 响应返回。它对应 OKHTTP 中的 execute 方法。
GET请求
/**
* 同步get方式请求
*
* @param url
* @return
* @throws IOException
*/
public static String doGet(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
}
POST请求
Json提交参数
/**
* 同步post方式请求-json提交参数
*
* @param url
* @param json
* @return
* @throws IOException
*/
public static String doPost(String url, final String json) throws IOException {
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
}
form表单提交参数
/**
* 同步post方式请求-form表单提交参数
*
* @param url
* @param paramsMap
* @return
* @throws IOException
*/
public static String doPost(String url, Map paramsMap) throws IOException {
OkHttpClient client = new OkHttpClient();
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
builder.add(key, paramsMap.get(key));
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
}
异步请求
就是类似于非阻塞式的请求,它的执行结果一般都是通过接口回调的方式告知调用者。它对应 OKHTTP 中的 enqueue 方法。
这是异步请求,所以调用enqueue则无需再开启子线程,enqueue方法会自动将网络请求部分放入子线程中执行。enqueue回调方法onResponse与onFailure都执行在子线程中。
注意事项:
-
- 回调接口的onFailure方法和onResponse执行在子线程。
-
- Response.code是http响应行中的code,如果访问成功则返回200.这个不是服务器设置的,而是http协议中自带的。res中的code才是服务器设置的。注意二者的区别。
-
- response.body().string()本质是输入流的读操作,所以它还是网络请求的一部分,所以这行代码必须放在子线程。
-
- response.body().string()只能调用一次,在第一次时有返回值,第二次再调用时将会返回null。原因是:response.body().string()的本质是输入流的读操作,必须有服务器的输出流的写操作时客户端的读操作才能得到数据。而服务器的写操作只执行一次,所以客户端的读操作也只能执行一次,第二次将返回null。
-
- 再次强调,response.body().string()方法必须放在子线程中。当执行这行代码得到结果后,再跳转到UI线程修改UI。
异步请求自定义回调函数
/**
* okhttp 异步调用回调函数
*/
static class OkHttpCallback implements Callback {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
log.error(e);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
if (response.isSuccessful()) {
log.info("Successful data acquisition . . . ");
log.info("response.code()==" + response.code());
log.info("response.body().string()==" + response.body().string());
}
}
}
GET请求
/**
* 异步get方式请求
*
* @param url
* @return
* @throws IOException
*/
public static void doSyncGet(String url) {
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
.url(url)
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new OkHttpCallback());
}
POST请求
Json提交参数
/**
* 异步post方式请求-json提交参数
*
* @param url
* @param json
* @return
* @throws IOException
*/
public static void doSyncPost(String url, final String json) {
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
client.newCall(request).enqueue(new OkHttpCallback());
}
form表单提交参数
/**
* 异步post方式请求-form表单提交参数
*
* @param url
* @param paramsMap
* @return
* @throws IOException
*/
public static void doSyncPost(String url, Map paramsMap) {
OkHttpClient client = new OkHttpClient();
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
builder.add(key, paramsMap.get(key));
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
client.newCall(request).enqueue(new OkHttpCallback());
}
参考
- https://www.jianshu.com/p/da4a806e599b
- https://blog.csdn.net/m0_38143863/article/details/80220247
示例代码
package com.ohaotian.feifz.style.study.utils;
import lombok.extern.log4j.Log4j2;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.Map;
/**
* @author feifz
* @version 1.0.0
* @Description http工具类,基于okhttp3
* @createTime 2019年06月06日 09:30:00
*/
@Log4j2
public class HttpUtil {
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
/**
* 同步get方式请求
*
* @param url
* @return
* @throws IOException
*/
public static String doGet(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
}
/**
* 异步get方式请求
*
* @param url
* @return
* @throws IOException
*/
public static void doSyncGet(String url) {
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
.url(url)
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new OkHttpCallback());
}
/**
* 同步post方式请求-json提交参数
*
* @param url
* @param json
* @return
* @throws IOException
*/
public static String doPost(String url, final String json) throws IOException {
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
}
/**
* 异步post方式请求-json提交参数
*
* @param url
* @param json
* @return
* @throws IOException
*/
public static void doSyncPost(String url, final String json) {
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
client.newCall(request).enqueue(new OkHttpCallback());
}
/**
* 同步post方式请求-form表单提交参数
*
* @param url
* @param paramsMap
* @return
* @throws IOException
*/
public static String doPost(String url, Map paramsMap) throws IOException {
OkHttpClient client = new OkHttpClient();
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
builder.add(key, paramsMap.get(key));
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
}
/**
* 异步post方式请求-form表单提交参数
*
* @param url
* @param paramsMap
* @return
* @throws IOException
*/
public static void doSyncPost(String url, Map paramsMap) {
OkHttpClient client = new OkHttpClient();
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
builder.add(key, paramsMap.get(key));
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
client.newCall(request).enqueue(new OkHttpCallback());
}
/**
* okhttp 异步调用回调函数
*/
static class OkHttpCallback implements Callback {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
log.error(e);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
if (response.isSuccessful()) {
log.info("Successful data acquisition . . . ");
log.info("response.code()==" + response.code());
log.info("response.body().string()==" + response.body().string());
}
}
}
}
结语
欢迎关注微信公众号『码仔zonE』,专注于分享Java、云计算相关内容,包括SpringBoot、SpringCloud、微服务、Docker、Kubernetes、Python等领域相关技术干货,期待与您相遇!