From d9872fdd71a571f1e5b18a6e54521525cbeee19b Mon Sep 17 00:00:00 2001 From: jwilson Date: Sat, 4 May 2013 18:09:38 -0400 Subject: [PATCH] Overview for the OkHttp website. --- website/index.html | 86 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/website/index.html b/website/index.html index 613b839e8..cdc0c3a2a 100644 --- a/website/index.html +++ b/website/index.html @@ -42,11 +42,89 @@
-

Introduction

-

Write me!

+

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

-

Write me!

+

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

@@ -82,7 +160,7 @@ limitations under the License.