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.

使用要求:对于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>
Gradle:
?
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
privatefinal OkHttpClient client = newOkHttpClient();
 
publicvoid run() throwsException {
  Request request = newRequest.Builder()
      .url(http://publicobject.com/helloworld.txt)
      .build();
 
  Response response = client.newCall(request).execute();
  if(!response.isSuccessful()) thrownew IOException(Unexpected code  + response);
 
  Headers responseHeaders = response.headers();
  for(inti = 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
privatefinal OkHttpClient client = newOkHttpClient();
 
publicvoid run() throwsException {
  Request request = newRequest.Builder()
      .url(http://publicobject.com/helloworld.txt)
      .build();
 
  client.newCall(request).enqueue(newCallback() {
    @Overridepublic void onFailure(Request request, Throwable throwable) {
      throwable.printStackTrace();
    }
 
    @Overridepublic void onResponse(Response response) throwsIOException {
      if(!response.isSuccessful()) thrownew IOException(Unexpected code  + response);
 
      Headers responseHeaders = response.headers();
      for(inti = 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
privatefinal OkHttpClient client = newOkHttpClient();
 
publicvoid run() throwsException {
  Request request = newRequest.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()) thrownew 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
publicstatic final MediaType jsonReq
    = MediaType.parse(application/json; charset=utf-8);
 
OkHttpClient client = newOkHttpClient();
 
String post(String url, String json) throwsIOException {
  RequestBody body = RequestBody.create(jsonReq, json);
  Request request = newRequest.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  returnresponse.body().string();
}

1 0
原创粉丝点击