okhttp:
HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.
大概意思就是更快,更省宽带。
OkHttp is an HTTP client that’s efficient by default:
okhttp是一个http客户端,默认情况下的效率。
1.HTTP/2 support allows all requests to the same host to share a socket.
http/2支持同一台主机的所有请求共享一个socket。
2.Connection pooling reduces request latency (if HTTP/2 isn’t available).
如果http/2是不可用的,连接池降低了请求延迟。
3.Transparent GZIP shrinks download sizes.
支持下载文件透明的GZIP压缩。
4.Response caching avoids the network completely for repeat requests.
响应缓存,完全避免重复请求。
Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks.
使用OkHttp很容易。它的请求/响应API的设计与流畅的建设者和永恒性。它同时支持同步阻塞调用和回调的异步调用。
OkHttp supports Android 2.3 and above. For Java, the minimum requirement is 1.7.
例子:
get请求:
This program downloads a URL and print its contents as a string
这是官方完整的get请求的例子。
package okhttp3.guide;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
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请求:
This program posts data to a service
官网完整 的post请求代码
package okhttp3.guide; import java.io.IOException; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class PostExample { public static final MediaType JSON = MediaType.parse("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); } }
这个是post提交json数据。
提交键值对也差不多,不过RequestBody修改下携带参数的方式。
<code class="hljs vbscript has-numbering"> RequestBody formBody = <span class="hljs-keyword">new</span> FormEncodingBuilder() .add(<span class="hljs-string">"platform"</span>, <span class="hljs-string">"android"</span>) .add(<span class="hljs-string">"xx"</span>, <span class="hljs-string">"zz"</span>) .add(<span class="hljs-string">"xx"</span>, <span class="hljs-string">"zz"</span>) ...参数 .build();</code>
配置:
You'll also need Okio, which OkHttp uses for fast I/O and resizable buffers. Download the latest JAR.
The source code to OkHttp, its samples, and this website is available on GitHub.
okhttp也需要Okio,下载jar之外也需要okio。Maven
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.4.1</version> </dependency>
Gradle
compile 'com.squareup.okhttp3:okhttp:3.4.1'