共计 6767 个字符,预计需要花费 17 分钟才能阅读完成。
Java 封装 OkHttp3 工具类,实用于 Java 后端开发者
说实在话,用过挺多网络申请工具,有过 java 原生的,HttpClient3 和 4,然而个人感觉用了 OkHttp3 之后,之前的那些齐全不想再用了。怎么说呢,代码轻便,应用起来很很很灵便,响应快,比起 HttpClient 好用许多。当然,这些是我个人观点,不喜勿喷。
筹备工作
Maven 我的项目在 pom 文件中引入 jar 包
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
引入 json 是因为工具类中有些中央用到了,当初通信都风行应用 json 传输,也少不了要这个 jar 包
工具类代码
import com.alibaba.fastjson.JSON;
import okhttp3.*;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class OkHttpUtils {
private static volatile OkHttpClient okHttpClient = null;
private static volatile Semaphore semaphore = null;
private Map<String, String> headerMap;
private Map<String, String> paramMap;
private String url;
private Request.Builder request;
/**
* 初始化 okHttpClient,并且容许 https 拜访
*/
private OkHttpUtils() {if (okHttpClient == null) {synchronized (OkHttpUtils.class) {if (okHttpClient == null) {TrustManager[] trustManagers = buildTrustManagers();
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0])
.hostnameVerifier((hostName, session) -> true)
.retryOnConnectionFailure(true)
.build();
addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
}
}
}
}
/**
* 用于异步申请时,管制拜访线程数,返回后果
*
* @return
*/
private static Semaphore getSemaphoreInstance() {
// 只能 1 个线程同时拜访
synchronized (OkHttpUtils.class) {if (semaphore == null) {semaphore = new Semaphore(0);
}
}
return semaphore;
}
/**
* 创立 OkHttpUtils
*
* @return
*/
public static OkHttpUtils builder() {return new OkHttpUtils();
}
/**
* 增加 url
*
* @param url
* @return
*/
public OkHttpUtils url(String url) {
this.url = url;
return this;
}
/**
* 增加参数
*
* @param key 参数名
* @param value 参数值
* @return
*/
public OkHttpUtils addParam(String key, String value) {if (paramMap == null) {paramMap = new LinkedHashMap<>(16);
}
paramMap.put(key, value);
return this;
}
/**
* 增加申请头
*
* @param key 参数名
* @param value 参数值
* @return
*/
public OkHttpUtils addHeader(String key, String value) {if (headerMap == null) {headerMap = new LinkedHashMap<>(16);
}
headerMap.put(key, value);
return this;
}
/**
* 初始化 get 办法
*
* @return
*/
public OkHttpUtils get() {request = new Request.Builder().get();
StringBuilder urlBuilder = new StringBuilder(url);
if (paramMap != null) {urlBuilder.append("?");
try {for (Map.Entry<String, String> entry : paramMap.entrySet()) {urlBuilder.append(URLEncoder.encode(entry.getKey(), "utf-8")).
append("=").
append(URLEncoder.encode(entry.getValue(), "utf-8")).
append("&");
}
} catch (Exception e) {e.printStackTrace();
}
urlBuilder.deleteCharAt(urlBuilder.length() - 1);
}
request.url(urlBuilder.toString());
return this;
}
/**
* 初始化 post 办法
*
* @param isJsonPost true 等于 json 的形式提交数据,相似 postman 里 post 办法的 raw
* false 等于一般的表单提交
* @return
*/
public OkHttpUtils post(boolean isJsonPost) {
RequestBody requestBody;
if (isJsonPost) {
String json = "";
if (paramMap != null) {json = JSON.toJSONString(paramMap);
}
requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
} else {FormBody.Builder formBody = new FormBody.Builder();
if (paramMap != null) {paramMap.forEach(formBody::add);
}
requestBody = formBody.build();}
request = new Request.Builder().post(requestBody).url(url);
return this;
}
/**
* 同步申请
*
* @return
*/
public String sync() {setHeader(request);
try {Response response = okHttpClient.newCall(request.build()).execute();
assert response.body() != null;
return response.body().string();
} catch (IOException e) {e.printStackTrace();
return "申请失败:" + e.getMessage();}
}
/**
* 异步申请,有返回值
*/
public String async() {StringBuilder buffer = new StringBuilder("");
setHeader(request);
okHttpClient.newCall(request.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {buffer.append("申请出错:").append(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {assert response.body() != null;
buffer.append(response.body().string());
getSemaphoreInstance().release();
}
});
try {getSemaphoreInstance().acquire();} catch (InterruptedException e) {e.printStackTrace();
}
return buffer.toString();}
/**
* 异步申请,带有接口回调
*
* @param callBack
*/
public void async(ICallBack callBack) {setHeader(request);
okHttpClient.newCall(request.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {callBack.onFailure(call, e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {assert response.body() != null;
callBack.onSuccessful(call, response.body().string());
}
});
}
/**
* 为 request 增加申请头
*
* @param request
*/
private void setHeader(Request.Builder request) {if (headerMap != null) {
try {for (Map.Entry<String, String> entry : headerMap.entrySet()) {request.addHeader(entry.getKey(), entry.getValue());
}
} catch (Exception e) {e.printStackTrace();
}
}
}
/**
* 生成安全套接字工厂,用于 https 申请的证书跳过
*
* @return
*/
private static SSLSocketFactory createSSLSocketFactory(TrustManager[] trustAllCerts) {
SSLSocketFactory ssfFactory = null;
try {SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
ssfFactory = sc.getSocketFactory();} catch (Exception e) {e.printStackTrace();
}
return ssfFactory;
}
private static TrustManager[] buildTrustManagers() {return new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) { }
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) { }
@Override
public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[]{};}
}
};
}
/**
* 自定义一个接口回调
*/
public interface ICallBack {void onSuccessful(Call call, String data);
void onFailure(Call call, String errorMsg);
}
}
应用教程
public static void main(String[] args) {
// get 申请,办法程序依照这种形式,切记抉择 post/get 肯定要放在倒数第二,同步或者异步倒数第一,才会正确执行
OkHttpUtils.builder().url("申请地址,http/https 都能够")
// 有参数的话增加参数,可多个
.addParam("参数名", "参数值")
.addParam("参数名", "参数值")
// 也能够增加多个
.addHeader("Content-Type", "application/json; charset=utf-8")
.get()
// 可抉择是同步申请还是异步申请
//.async();
.sync();
// post 申请,分为两种,一种是一般表单提交,一种是 json 提交
OkHttpUtils.builder().url("申请地址,http/https 都能够")
// 有参数的话增加参数,可多个
.addParam("参数名", "参数值")
.addParam("参数名", "参数值")
// 也能够增加多个
.addHeader("Content-Type", "application/json; charset=utf-8")
// 如果是 true 的话,会相似于 postman 中 post 提交形式的 raw,用 json 的形式提交,不是表单
// 如果是 false 的话传统的表单提交
.post(true)
.sync();
// 抉择异步有两个办法,一个是带回调接口,一个是间接返回后果
OkHttpUtils.builder().url("")
.post(false)
.async();
OkHttpUtils.builder().url("").post(false).async(new OkHttpUtils.ICallBack() {
@Override
public void onSuccessful(Call call, String data) {// 申请胜利后的解决}
@Override
public void onFailure(Call call, String errorMsg) {// 申请失败后的解决}
});
}
结语
封装的明明白白,应用的简简单单,简略的几下就能做申请,用建造者模式是真的难受
作者:如漩涡
起源:https://blog.csdn.net/m0_3770…
正文完