HTTP是现代应用常用的一种交换数据和媒体的网络方式,高效地使用HTTP能让资源加载更快,节省带宽。OkHttp是一个高效的HTTP客户端,它有以下默认特性:
源码: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依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.0.0-RC1</version>
</dependency>
就是执行请求的操作是阻塞式,直到 HTTP 响应返回。它对应 OKHTTP 中的 execute 方法。
/**
* 同步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提交参数
*
* @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方式请求-form表单提交参数
*
* @param url
* @param paramsMap
* @return
* @throws IOException
*/
public static String doPost(String url, Map<String, String> 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都执行在子线程中。
注意事项:
异步请求自定义回调函数
/**
* 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方式请求
*
* @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 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 void doSyncPost(String url, Map<String, String> 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());
}
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<String, String> 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<String, String> 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等领域相关技术干货,期待与您相遇!
原文:https://www.cnblogs.com/feifuzeng/p/13554288.html