An HTTP & SPDY client for Android and Java applications

Overview

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:

  • SPDY support allows all requests to the same host to share a socket.
  • Connection pooling reduces request latency (if SPDY isn’t available).
  • Transparent GZIP shrinks download sizes.
  • Response caching avoids the network completely for repeat requests.

OkHttp perseveres when the network is troublesome: it will silently recover from common connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6 and for services hosted in redundant data centers. OkHttp also recovers from problematic proxy servers and failed SSL handshakes.

You can try OkHttp without rewriting your network code. The core module implements the familiar java.net.HttpURLConnection API. And the optional okhttp-apache module implements the Apache HttpClient API.

OkHttp supports Android 2.2 and above. For Java, the minimum requirement is 1.5.

Examples

Get a URL

This program downloads a URL and print its contents as a string. Full source.

    OkHttpClient client = new OkHttpClient();

    String get(URL url) throws IOException {
      HttpURLConnection connection = client.open(url);
      InputStream in = null;
      try {
        // Read the response.
        in = connection.getInputStream();
        byte[] response = readFully(in);
        return new String(response, "UTF-8");
      } finally {
        if (in != null) in.close();
      }
    }

Post to a Server

This program posts data to a service. Full source.

    OkHttpClient client = new OkHttpClient();

    String post(URL url, byte[] body) throws IOException {
      HttpURLConnection connection = client.open(url);
      OutputStream out = null;
      InputStream in = null;
      try {
        // Write the request.
        connection.setRequestMethod("POST");
        out = connection.getOutputStream();
        out.write(body);
        out.close();

        // Read the response.
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
          throw new IOException("Unexpected HTTP response: "
              + connection.getResponseCode() + " " + connection.getResponseMessage());
        }
        in = connection.getInputStream();
        return readFirstLine(in);
      } finally {
        // Clean up.
        if (out != null) out.close();
        if (in != null) in.close();
      }
    }

Download

Latest JAR

The source code to the OkHttp, its samples, and this website is available on GitHub.

Maven

<dependency>
  <groupId>com.squareup.okhttp</groupId>
  <artifactId>okhttp</artifactId>
  <version>(insert latest version)</version>
</dependency>

Contributing

If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request.

When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. Please also make sure your code compiles by running mvn clean verify.

Before your code can be accepted into the project you must also sign the Individual Contributor License Agreement (CLA).

License

Copyright 2013 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.