okHttp: OKHttp是Android版Http客户端。非常高效,支持SPDY、连接池、GZIP和 HTTP 缓存。默认情况下,OKHttp会自动处理常见的网络问题,像二次连接、SSL的握手问题。如果你的应用程序中集成了OKHttp,Retrofit默认会使用OKHttp处理其他网络层请求。
An HTTP & SPDY client for Android and Java applications 从Android4.4开始HttpURLConnection的底层实现采用的是okHttp.
Gradle:
An HTTP & SPDY client for Android and Java applications 从Android4.4开始HttpURLConnection的底层实现采用的是okHttp.
使用要求:对于Android:2.3以上,对于Java:java7以上 两个模块: okhttp-urlconnection实现.HttpURLConnection API; okhttp-apache实现Apache HttpClient API. 依赖:okio(https://github.com/square/okio): Okio, which OkHttp uses for fast I/O and resizable buffers.
安装:
maven:
1
2
3
4
5
|
<dependency>
<groupid>com.squareup.okhttp</groupid>
okhttp</artifactid>
<version>
2.3
.
0
</version>
</dependency>
|
1
|
compile
'com.squareup.okhttp:okhttp:2.3.0'
|
GET A URL
同步GET:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private
final
OkHttpClient client =
new
OkHttpClient();
public
void
run()
throws
Exception {
Request request =
new
Request.Builder()
.url(http:
//publicobject.com/helloworld.txt)
.build();
Response response = client.newCall(request).execute();
if
(!response.isSuccessful())
throw
new
IOException(Unexpected code + response);
Headers responseHeaders = response.headers();
for
(
int
i =
0
; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + : + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
|
异步GET:
在一个工作线程中下载文件,当响应可读时回调Callback接口。读取响应时会阻塞当前线程。OkHttp现阶段不提供异步api来接收响应体。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
private
final
OkHttpClient client =
new
OkHttpClient();
public
void
run()
throws
Exception {
Request request =
new
Request.Builder()
.url(http:
//publicobject.com/helloworld.txt)
.build();
client.newCall(request).enqueue(
new
Callback() {
@Override
public
void
onFailure(Request request, Throwable throwable) {
throwable.printStackTrace();
}
@Override
public
void
onResponse(Response response)
throws
IOException {
if
(!response.isSuccessful())
throw
new
IOException(Unexpected code + response);
Headers responseHeaders = response.headers();
for
(
int
i =
0
; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + : + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
});
}
|
访问Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private
final
OkHttpClient client =
new
OkHttpClient();
public
void
run()
throws
Exception {
Request request =
new
Request.Builder()
.url(https:
//api.github.com/repos/square/okhttp/issues)
.header(User-Agent, OkHttp Headers.java)
.addHeader(Accept, application/json; q=
0.5
)
.addHeader(Accept, application/vnd.github.v3+json)
.build();
Response response = client.newCall(request).execute();
if
(!response.isSuccessful())
throw
new
IOException(Unexpected code + response);
System.out.println(Server: + response.header(Server));
System.out.println(Date: + response.header(Date));
System.out.println(Vary: + response.headers(Vary));
}
|
Posting a String:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
static
final
MediaType jsonReq
= MediaType.parse(application/json; charset=utf-
8
);
OkHttpClient client =
new
OkHttpClient();
String post(String url, String json)
throws
IOException {
RequestBody body = RequestBody.create(jsonReq, json);
Request request =
new
Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return
response.body().string();
}
|