什么是OKHttp
一般在Java平台上,我们会使用Apache HttpClient作为Http客户端,用于发送 HTTP 请求,并对响应进行处理。比如可以使用http客户端与第三方服务(如SSO服务)进行集成,当然还可以爬取网上的数据等。OKHttp与HttpClient类似,也是一个Http客户端,提供了对 HTTP/2 和 SPDY 的支持,并提供了连接池,GZIP 压缩和 HTTP 响应缓存功能;
OkHttp是目前非常火的网络库,它有以下特性:
1.支持HTTP/2,允许所有同一个主机地址的请求共享同一个socket连接
2.连接池减少请求延时
3.透明的GZIP压缩减少响应数据的大小
4.缓存响应内容,避免一些完全重复的请求
OkHttp基本使用
OkHttpClient:客户端对象 Request:访问请求,Post请求中需要包含RequestBody RequestBody:请求数据,在Post请求中用到 Response:即网络请求的响应结果 MediaType:数据类型,用来表明数据是json,image,pdf等一系列格式 client.newCall(request).execute():同步的请求方法 client.newCall(request).enqueue(Callback callBack):异步的请求方法,但Callback是执行在子线程中的,因此不能在此进行UI更新操作官网: http://square.github.io/okhttp/
添加依赖
1 2 3 4 5 6 7 8 9 10 11 |
<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> |
OkHttp工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
package com.loginsimpl.utils;
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);
} } |
使用案例
发送get请求
1 2 3 4 5 6 |
String sync = OkHttpUtils.builder().url( "http://localhost:9100/user/all" ) .addHeader( "token" , "xxxxxxx" ) .addParam( "name" , "xxx" ).addParam( "pass" , "xxx" ) .get() .sync(); System.out.println(sync); |
发送Post请求
1 2 3 4 5 6 |
String sync = OkHttpUtils.builder().url( "http://localhost:9100/user/all" ) .addHeader( "token" , "xxxxxxx" ) .addParam( "name" , "xxx" ).addParam( "pass" , "xxx" ) .post( true ) // true 为json提交方式 .sync(); System.out.println(sync); |
发送异步请求
需要实现ICallBack接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//回调实现类 public class AsyncHttpServiceImpl implements OkHttpUtils.ICallBack { @Override public void onSuccessful(Call call, String data) { //接口正常返回的内容 System.out.println( "onSuccessful" +data);
}
@Override public void onFailure(Call call, String errorMsg) { //接口错误返回的内容 System.out.println( "onFailure" +errorMsg); } } |
发送异步请求
1 2 3 4 5 |
OkHttpUtils.builder().url( "http://localhost:9100/user/all" ) .addHeader( "token" , "xxxxxxx" ) .addParam( "name" , "xxx" ).addParam( "pass" , "xxx" ) .post( true ) .async( new AsyncHttpServiceImpl()); |
到此这篇关于Java-OkHttp使用教程的文章就介绍到这了,更多相关Java-OkHttp使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/weixin_45203607/article/details/124204137
查看更多关于Java中的OkHttp使用教程的详细内容...