OkHTTP框架
来源:互联网 发布:java线程池完整实例 编辑:程序博客网 时间:2024/05/28 15:20
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();
}
1 0
- oKhttp框架
- OkHTTP框架
- OKHttp框架
- OkHttp框架
- OkHttp框架
- OKHttp框架
- Android OkHttp封装 框架
- okHttp开发框架使用
- android OkHttp框架解析
- okhttp框架的封装
- okhttp框架的使用
- okhttp网络请求框架
- java-框架-OkHttp
- 网络请求框架OKHttp
- OkHttp网络请求框架
- Android OkHttp框架解析
- okhttp框架 工具类
- Android okHttp框架
- 我是一条SQL
- 哈夫曼树和哈夫曼编码
- 使用Maven+Tomcat实现项目的热部署
- 关于unity使用dll和c#扩展方法的小结
- IE异常
- OkHTTP框架
- 一个响应式数据库框架SQLBrite,完美解决数据库和UI的同步更新!
- bzoj 1863: [Zjoi2006]trouble 皇帝的烦恼(二分答案+dp)
- 滴滴笔试题
- 沈阳网赛1010 HDU 5901 Count primes
- error opening trace file: No such file or directory错误
- 关押罪犯
- 终于开通自己的博客了,开始编程之路
- 面试第二篇