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 @@
Write me!
+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 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.
Write me!
+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();
+ }
+ }
+
+ 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();
+ }
+ }
+
+
+