Home | History | Annotate | Download | only in website
      1 <!DOCTYPE html>
      2 <html lang="en">
      3   <head>
      4     <meta charset="utf-8">
      5     <title>OkHttp</title>
      6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
      7     <meta name="description" content="An HTTP &amp; SPDY client for Android and Java applications">
      8     <link href="static/bootstrap-combined.min.css" rel="stylesheet">
      9     <link href="static/app.css" rel="stylesheet">
     10     <link href="static/app-theme.css" rel="stylesheet">
     11     <link href="http://fonts.googleapis.com/css?family=Roboto:400,300italic,100,100italic,300" rel="stylesheet" type="text/css">
     12     <!--[if lt IE 9]><script src="static/html5shiv.min.js"></script><![endif]-->
     13   </head>
     14   <body data-target=".content-nav">
     15     <header>
     16       <div class="container">
     17         <div class="row">
     18           <div class="span5">
     19             <h1>OkHttp</h1>
     20           </div>
     21           <div class="span7">
     22             <menu>
     23               <ul>
     24                 <li><a href="#download" class="menu download">Download <span class="version-tag">Latest</span></a></li>
     25                 <li><a href="http://github.com/square/okhttp" data-title="View GitHub Project" class="menu github"><img src="static/icon-github.png" alt="GitHub"/></a></li>
     26                 <li><a href="http://square.github.io/" data-title="Square Open Source Portal" class="menu square"><img src="static/icon-square.png" alt="Square"/></a></li>
     27               </ul>
     28             </menu>
     29           </div>
     30       </div>
     31     </header>
     32     <section id="subtitle">
     33       <div class="container">
     34         <div class="row">
     35           <div class="span12">
     36             <h2>An <strong>HTTP &amp; SPDY</strong> client for Android and Java applications</h2>
     37           </div>
     38         </div>
     39       </div>
     40     </section>
     41     <section id="body">
     42       <div class="container">
     43         <div class="row">
     44           <div class="span9">
     45             <h3 id="overview">Overview</h3>
     46             <p>HTTP is the way modern applications network. Its how we exchange data & media.
     47                 Doing HTTP efficiently makes your stuff load faster and saves bandwidth.</p>
     48 
     49             <p>OkHttp is an HTTP client thats efficient by default:</p>
     50             <ul>
     51                 <li>SPDY support allows all requests to the same host to share a socket.</li>
     52                 <li>Connection pooling reduces request latency (if SPDY isnt available).</li>
     53                 <li>Transparent GZIP shrinks download sizes.</li>
     54                 <li>Response caching avoids the network completely for repeat requests.</li>
     55             </ul>
     56 
     57             <p>OkHttp perseveres when the network is troublesome: it will silently recover from
     58                 common connection problems. If your service has multiple IP addresses OkHttp will
     59                 attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6
     60                 and for services hosted in redundant data centers. OkHttp also recovers from problematic
     61                 proxy servers and failed SSL handshakes.</p>
     62 
     63             <p>You can try OkHttp without rewriting your network code. The core module implements
     64                 the familiar <code>java.net.HttpURLConnection</code> API. And the optional
     65                 okhttp-apache module implements the Apache <code>HttpClient</code> API.</p>
     66 
     67             <p>OkHttp supports Android 2.2 and above. For Java, the minimum requirement is 1.5.</p>
     68 
     69             <h3 id="examples">Examples</h3>
     70             <h4>Get a URL</h4>
     71             <p>This program downloads a URL and print its contents as a string. <a href="https://raw.github.com/square/okhttp/master/samples/guide/src/main/java/com/squareup/okhttp/guide/GetExample.java">Full source</a>.
     72 <pre class="prettyprint">
     73     OkHttpClient client = new OkHttpClient();
     74 
     75     String get(URL url) throws IOException {
     76       HttpURLConnection connection = client.open(url);
     77       InputStream in = null;
     78       try {
     79         // Read the response.
     80         in = connection.getInputStream();
     81         byte[] response = readFully(in);
     82         return new String(response, "UTF-8");
     83       } finally {
     84         if (in != null) in.close();
     85       }
     86     }
     87 </pre>
     88             <h4>Post to a Server</h4>
     89             <p>This program posts data to a service. <a href="https://raw.github.com/square/okhttp/master/samples/guide/src/main/java/com/squareup/okhttp/guide/PostExample.java">Full source</a>.
     90 
     91 <pre class="prettyprint">
     92     OkHttpClient client = new OkHttpClient();
     93 
     94     String post(URL url, byte[] body) throws IOException {
     95       HttpURLConnection connection = client.open(url);
     96       OutputStream out = null;
     97       InputStream in = null;
     98       try {
     99         // Write the request.
    100         connection.setRequestMethod("POST");
    101         out = connection.getOutputStream();
    102         out.write(body);
    103         out.close();
    104 
    105         // Read the response.
    106         if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
    107           throw new IOException("Unexpected HTTP response: "
    108               + connection.getResponseCode() + " " + connection.getResponseMessage());
    109         }
    110         in = connection.getInputStream();
    111         return readFirstLine(in);
    112       } finally {
    113         // Clean up.
    114         if (out != null) out.close();
    115         if (in != null) in.close();
    116       }
    117     }
    118 </pre>
    119 
    120                 <!--
    121                 TODO
    122                 Error Handling
    123                 Authentication
    124                 Cookies
    125                 Response Caching
    126                 Captive Gateways
    127                 -->
    128 
    129             <h3 id="download">Download</h3>
    130             <p><a href="http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.squareup.okhttp&a=okhttp&v=LATEST" class="dl version-href">&darr; <span class="version-tag">Latest</span> JAR</a></p>
    131             <p>The source code to the OkHttp, its samples, and this website is <a href="http://github.com/square/okhttp">available on GitHub</a>.</p>
    132 
    133             <h4>Maven</h4>
    134             <pre class="prettyprint">&lt;dependency>
    135   &lt;groupId>com.squareup.okhttp&lt;/groupId>
    136   &lt;artifactId>okhttp&lt;/artifactId>
    137   &lt;version><span class="version pln"><em>(insert latest version)</em></span>&lt;/version>
    138 &lt;/dependency></pre>
    139 
    140             <h3 id="contributing">Contributing</h3>
    141             <p>If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request.</p>
    142             <p>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 <code>mvn clean verify</code>.</p>
    143             <p>Before your code can be accepted into the project you must also sign the <a href="http://squ.re/sign-the-cla">Individual Contributor License Agreement (CLA)</a>.</p>
    144 
    145             <h3 id="license">License</h3>
    146             <pre>Copyright 2013 Square, Inc.
    147 
    148 Licensed under the Apache License, Version 2.0 (the "License");
    149 you may not use this file except in compliance with the License.
    150 You may obtain a copy of the License at
    151 
    152    http://www.apache.org/licenses/LICENSE-2.0
    153 
    154 Unless required by applicable law or agreed to in writing, software
    155 distributed under the License is distributed on an "AS IS" BASIS,
    156 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    157 See the License for the specific language governing permissions and
    158 limitations under the License.</pre>
    159           </div>
    160           <div class="span3">
    161             <div class="content-nav" data-spy="affix" data-offset-top="80">
    162               <ul class="nav nav-tabs nav-stacked primary">
    163                 <li><a href="#overview">Overview</a></li>
    164                 <li><a href="#examples">Examples</a></li>
    165                 <li><a href="#download">Download</a></li>
    166                 <li><a href="#contributing">Contributing</a></li>
    167                 <li><a href="#license">License</a></li>
    168               </ul>
    169               <ul class="nav nav-pills nav-stacked secondary">
    170                 <li><a href="javadoc/index.html">Javadoc</a></li>
    171                 <li><a href="http://stackoverflow.com/questions/tagged/okhttp?sort=active">StackOverflow</a></li>
    172               </ul>
    173             </div>
    174           </div>
    175         </div>
    176         <div class="row">
    177           <div class="span12 logo">
    178             <a href="https://squareup.com"><img src="static/logo-square.png" alt="Square, Inc."/></a>
    179           </div>
    180         </div>
    181       </div>
    182     </section>
    183     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    184     <script src="static/bootstrap.min.js"></script>
    185     <script src="static/jquery.smooth-scroll.min.js"></script>
    186     <script src="static/jquery-maven-artifact.min.js"></script>
    187     <script src="static/prettify.js"></script>
    188     <script type="text/javascript">
    189       $(function() {
    190         // Syntax highlight code blocks.
    191         prettyPrint();
    192 
    193         // Spy on scroll position for real-time updating of current section.
    194         $('body').scrollspy();
    195 
    196         // Use smooth-scroll for internal links.
    197         $('a').smoothScroll();
    198 
    199         // Enable tooltips on the header nav image items.
    200         $('.menu').tooltip({
    201           placement: 'bottom',
    202           trigger: 'hover',
    203           container: 'body',
    204           delay: {
    205             show: 500,
    206             hide: 0
    207           }
    208         });
    209 
    210         // Look up the latest version of the library.
    211         $.fn.artifactVersion({
    212           'groupId': 'com.squareup.okhttp',
    213           'artifactId': 'okhttp'
    214         }, function(version, url) {
    215           $('.version').text(version);
    216           $('.version-tag').text('v' + version);
    217           $('.version-href').attr('href', url);
    218         });
    219       });
    220 
    221       var _gaq = _gaq || [];
    222       _gaq.push(['_setAccount', 'UA-40704740-2']);
    223       _gaq.push(['_trackPageview']);
    224 
    225       (function() {
    226         var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    227         ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    228         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    229       })();
    230     </script>
    231   </body>
    232 </html>
    233