OkHttp好处
OkHttp支持HTTP1.x和HTTP2 OkHttp相对于其他HTTP客户端有着明显的优势: HTTP2支持允许对同一主机的所有请求共享套接字 连接池减少了请求延迟(如果HTTP2不可用) 透明GZIP缩小了下载大小 相应缓存可以完全避免网络重复请求 当网络有错误时,它能从常见的连接问题中回复过来 如果你的服务有多个IP地址,如果第一次连接失败,OkHttp将尝试备用地址 OkHttp支持现代TLS功能
OkHttp使用
Get请求
public class GetExample {
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static void main(String[] args) throws IOException {
GetExample example = new GetExample();
String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
System.out.println(response);
}
}
Post请求
public class PostExample {
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
String bowlingJson(String player1, String player2) {
return "{'winCondition':'HIGH_SCORE',"
+ "'name':'Bowling',"
+ "'round':4,"
+ "'lastSaved':1367702411696,"
+ "'dateStarted':1367702378785,"
+ "'players':["
+ "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39},"
+ "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}"
+ "]}";
}
public static void main(String[] args) throws IOException {
PostExample example = new PostExample();
String json = example.bowlingJson("Jesse", "Jake");
String response = example.post("http://www.roundsapp.com/post", json);
System.out.println(response);
}
}
实现WebSocket通信客户端
String wsUrl = "ws://" + hostName + ":" + port + "/"
//新建client
OkHttpClient client = new OkHttpClient.Builder().build();
//构造request对象
Request request = new Request.Builder().url(wsUrl).build();
//建立WebSocket连接
WebSocket webSocket = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
// 打开链接后回调
mWebSocket = webSocket;
System.out.println("client onOpen");
System.out.println("client request header:" + response.request().headers());
System.out.println("client response header:" + response.headers());
System.out.println("client response:" + response);
//开启消息定时发送
startTask();
}
@Override
public void onMessage(WebSocket webSocket, String text) {
// 返回数据回调
System.out.println("client onMessage");
System.out.println("message:" + text);
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
// 正在关闭连接回调
System.out.println("client onClosing");
System.out.println("code:" + code + " reason:" + reason);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
// 关闭连接后回调
System.out.println("client onClosed");
System.out.println("code:" + code + " reason:" + reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
//出现异常会进入此回调
System.out.println("client onFailure");
System.out.println("throwable:" + t);
System.out.println("response:" + response);
}
});
// 发送数据
webSocket.send("")