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:
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:
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 our HttpURLConnection
API which moved to a new okhttp-urlconnection
artifact.
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.
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.
Get 2.0.0-RC1 from Maven Central:
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.0.0-RC1</version>
</dependency>