轉載自:https://publicobject.com/2014/05/24/okhttp-2-0-rc1/java
Adrian, Jake and I have been working on OkHttp 2.0 quite actively for nearly a year, and we're finally ready to share its new API with the world:git
OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://square.com/") .build(); Response response = client.newCall(request).execute(); System.out.println(response.body().string());
The 2.0 API leverages fluent builders and immutability to make HTTP easy. It can be used synchronously (above) or asynchronously:github
OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://square.com") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onResponse(Response response) throws IOException { System.out.println(response.body().string()); } @Override public void onFailure(Request r, Throwable e) { ... } });
It shares the same sophisticated backend as ourHttpURLConnection
API which moved to a new okhttp-urlconnection
artifact.async
OkHttp 2.0 is not backwards-compatible. The changelog describes what's changed and what's gone. To make upgrading easier, we're also releasing OkHttp 1.6 which is the 1.5 code plus some new 2.0 APIs. Use 1.6 to transition to 2.x APIs.ide
Today we're publishing a release-candidate with the goal of a final release in June. Between now and then we're going to update the examples, complete the documentation, and fix any bugs that are reported.post
Get 2.0.0-RC1 from Maven Central:ui
<dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>okhttp</artifactId> <version>2.0.0-RC1</version> </dependency>