OkHttp–支持SPDY协议的高效HTTP库
来源:互联网 发布:淘宝店铺音乐怎么换 编辑:程序博客网 时间:2024/05/22 05:19
Android为我们提供了两种HTTP交互的方式: HttpURLConnection 和 Apache HTTP Client,虽然两者都支持HTTPS,流的上传和下载,配置超时,IPv6和连接池,已足够满足我们各种HTTP请求的需求。但更高效的使用HTTP可以让您的应用运行更快、更节省流量。而OkHttp库就是为此而生。
OkHttp是一个高效的HTTP库:
- 支持 SPDY ,共享同一个Socket来处理同一个服务器的所有请求
- 如果SPDY不可用,则通过连接池来减少请求延时
- 无缝的支持GZIP来减少数据流量
- 缓存响应数据来减少重复的网络请求
会从很多常用的连接问题中自动恢复。如果您的服务器配置了多个IP地址,当第一个IP连接失败的时候,OkHttp会自动尝试下一个IP。OkHttp还处理了代理服务器问题和SSL握手失败问题。
使用 OkHttp 无需重写您程序中的网络代码。OkHttp实现了几乎和java.net.HttpURLConnection一样的API。如果您用了 Apache HttpClient,则OkHttp也提供了一个对应的okhttp-apache 模块。
Examples
下面的示例请求一个URL并答应出返回内容字符.
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
package com.squareup.okhttp.guide;
import com.squareup.okhttp.OkHttpClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetExample {
OkHttpClient client = new OkHttpClient();
void run() throws IOException {
String result = get(new URL("https://raw.github.com/square/okhttp/master/README.md"));
System.out.println(result);
}
String get(URL url) throws IOException {
HttpURLConnection connection = client.open(url);
InputStream in = null;
try {
// Read the response.
in = connection.getInputStream();
byte[] response = readFully(in);
return new String(response, "UTF-8");
} finally {
if (in != null) in.close();
}
}
byte[] readFully(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int count; (count = in.read(buffer)) != -1; ) {
out.write(buffer, 0, count);
}
return out.toByteArray();
}
public static void main(String[] args) throws IOException {
new GetExample().run();
}
}
下面的代码通过Post发送数据到服务器:
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
package com.squareup.okhttp.guide;
import com.squareup.okhttp.OkHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class PostExample {
OkHttpClient client = new OkHttpClient();
void run() throws IOException {
byte[] body = bowlingJson("Jesse", "Jake").getBytes("UTF-8");
String result = post(new URL("http://www.roundsapp.com/post"), body);
System.out.println(result);
}
String post(URL url, byte[] body) throws IOException {
HttpURLConnection connection = client.open(url);
OutputStream out = null;
InputStream in = null;
try {
// Write the request.
connection.setRequestMethod("POST");
out = connection.getOutputStream();
out.write(body);
out.close();
// Read the response.
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("Unexpected HTTP response: "
+ connection.getResponseCode() + " " + connection.getResponseMessage());
}
in = connection.getInputStream();
return readFirstLine(in);
} finally {
// Clean up.
if (out != null) out.close();
if (in != null) in.close();
}
}
String readFirstLine(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
return reader.readLine();
}
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 {
new PostExample().run();
}
}
参考:
http://square.github.io/okhttp/
http://android-developers.blogspot.com/2011/09/androids-http-clients.html
转自:http://liuzhichao.com/p/1707.html
0 0
- OkHttp–支持SPDY协议的高效HTTP库
- OkHttp –Android、Java应用中的 HTTP & SPDY 客户端库
- OkHttp –Android、Java应用中的 HTTP & SPDY 客户端库
- SPDY:Google开发的下一代HTTP协议
- SPDY协议的Android实现 OKHTTP代码分析
- OKHttp源码解析-ConnectionPool对Connection重用机制&Http/Https/SPDY协议选择
- Android中Okhttp和SPDY协议
- 安装支持spdy的nginx并且通过wireshark解密spdy协议
- Linux Mint下编译安装支持SPDY协议的Wireshark
- HTTP代理与SPDY协议
- SPDY、HTTP/2、QUIC协议
- HTTP代理与SPDY协议
- SPDY、HTTP/2、QUIC协议
- nginx的spdy协议
- Nginx下让SSL支持SPDY协议
- Nginx下让SSL支持SPDY协议
- OkHttp完全解析(七)SPDY协议详细介绍
- OkHttp完全解析(七)SPDY协议详细介绍
- HDU3086-追及问题-解一元二次方程组
- 在iOS上使用ffmpeg播放视频
- 归去来
- 编译Android下可用的FFmpeg+x264
- webgrid 用法 asp.net mvc
- OkHttp–支持SPDY协议的高效HTTP库
- HDU3339 最短路加01背包
- DroidDraw使用
- hibernate一对一
- poj 2184 Cow Exhibition(01背包变形)
- UVA 10730 - Antiarithmetic?(技巧枚举)
- android对xml的各种解析方法
- OpenSIPS and Control Panel Install Guide
- WIN32 DLL中使用MFC的问题