# OkHttp使用教程
<p>Android系统提供了两种HTTP通信类,HttpURLConnection和HttpClient。<br><a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">关于HttpURLConnection和HttpClient的选择>>官方博客</a><br>尽管Google在大部分安卓版本中推荐使用HttpURLConnection,但是这个类相比HttpClient实在是太难用,太弱爆了。<br>OkHttp是一个相对成熟的解决方案,据说Android4.4的源码中可以看到HttpURLConnection已经替换成OkHttp实现了。所以我们更有理由相信OkHttp的强大。</p><p>OkHttp 处理了很多网络疑难杂症:会从很多常用的连接问题中自动恢复。如果您的服务器配置了多个IP地址,当第一个IP连接失败的时候,OkHttp会自动尝试下一个IP。OkHttp还处理了代理服务器问题和SSL握手失败问题。</p><p>使用 OkHttp 无需重写您程序中的网络代码。OkHttp实现了几乎和java.net.HttpURLConnection一样的API。如果你用了 Apache HttpClient,则OkHttp也提供了一个对应的okhttp-apache 模块。</p><p><br></p> <p class="runtimead">
</p> <blockquote><p>注:在国内使用OkHttp会因为这个问题导致部分酷派手机用户无法联网,所以对于大众app来说,需要等待这个bug修复后再使用。或者尝试使用OkHttp的老版本。<br>截止到目前,OkHttp一直没有修复,并把修复计划延迟到了OkHttp2.3中。不是所有设备都能重现,仅少量设备会出现这个问题。(如果问题这么明显,OkHttp早就修复了)</p></blockquote><h1>入门</h1><h2>官方资料</h2><p><a href="http://square.github.io/okhttp/">官方介绍</a><br><a href="https://github.com/square/okhttp">github源码</a></p><h2>使用范围</h2><p>OkHttp支持Android 2.3及其以上版本。<br>对于Java, JDK1.7以上。</p><h2 id="jar">jar包准备</h2><p>官方介绍页面有链接位置。这里把下载链接也写在下面。<br><a href="http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.squareup.okhttp&a=okhttp&v=LATEST">OkHttp</a><br><a href="http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.squareup.okio&a=okio&v=LATEST">Okio</a></p><h2>基本使用</h2><h3 id="http-get">HTTP GET</h3><div><div id="highlighter_279020" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="js plain">String run(String url) throws IOException {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder().url(url).build();</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute(); </code><code class="js keyword">if</code> <code class="js plain">(response.isSuccessful()) { </code><code class="js keyword">return</code> <code class="js plain">response.body().string();</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">} </code><code class="js keyword">else</code> <code class="js plain">{ </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number8 index7 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><p>Request是OkHttp中访问的请求,Builder是辅助类。Response即OkHttp中的响应。</p><h4 id="response">Response类:</h4><div><div id="highlighter_633330" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">public boolean isSuccessful()</code></div><div class="line number2 index1 alt1"><code class="js plain">Returns </code><code class="js keyword">true</code> <code class="js keyword">if</code> <code class="js plain">the code is </code><code class="js keyword">in</code> <code class="js plain">[200..300),</code></div><div class="line number3 index2 alt2"><code class="js spaces"> </code><code class="js plain">which means the request was successfully received, understood, and accepted.</code></div></div></td></tr></tbody></table></div></div><h4 id="response.bodyresponsebody">response.body()返回ResponseBody类</h4><p>可以方便的获取string</p><div><div id="highlighter_692410" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">public final String string() throws IOException</code></div><div class="line number2 index1 alt1"><code class="js plain">Returns the response as a string decoded </code><code class="js keyword">with</code> <code class="js plain">the charset of the Content-Type header. If that header is either absent or lacks a charset,</code></div><div class="line number3 index2 alt2"><code class="js spaces"> </code><code class="js keyword">this</code> <code class="js plain">will attempt to decode the response body as UTF-8.Throws:</code></div><div class="line number4 index3 alt1"><code class="js plain">IOException</code></div></div></td></tr></tbody></table></div></div><p>当然也能获取到流的形式:</p><div><div id="highlighter_617123" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">public final InputStream byteStream()</code></div></div></td></tr></tbody></table></div></div><h3 id="http-post">HTTP POST</h3><h4 id="postjson">POST提交Json数据</h4><div><div id="highlighter_145107" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">public static final MediaType JSON = MediaType.parse(</code><code class="js string">"application/json; charset=utf-8"</code><code class="js plain">);</code></div><div class="line number2 index1 alt1"><code class="js plain">OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number3 index2 alt2"><code class="js plain">String post(String url, String json) throws IOException {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">RequestBody body = RequestBody.create(JSON, json);</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">.url(url)</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">.post(body)</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">f (response.isSuccessful()) {</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">response.body().string();</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">} </code><code class="js keyword">else</code> <code class="js plain">{</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number15 index14 alt2"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><p>使用Request的post方法来提交请求体RequestBody</p><h4 id="post">POST提交键值对</h4><p>很多时候我们会需要通过POST方式把键值对数据传送到服务器。 OkHttp提供了很方便的方式来做这件事情。</p><div><div id="highlighter_283837" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"><code class="js plain">String post(String url, String json) throws IOException {</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">RequestBody formBody = </code><code class="js keyword">new</code> <code class="js plain">FormEncodingBuilder()</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">.add(</code><code class="js string">"platform"</code><code class="js plain">, </code><code class="js string">"android"</code><code class="js plain">)</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">.add(</code><code class="js string">"name"</code><code class="js plain">, </code><code class="js string">"bug"</code><code class="js plain">)</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">.add(</code><code class="js string">"subject"</code><code class="js plain">, </code><code class="js string">"XXXXXXXXXXXXXXX"</code><code class="js plain">)</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">.url(url)</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">.post(body)</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number14 index13 alt1"> </div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(response.isSuccessful()) {</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">response.body().string();</code></div><div class="line number18 index17 alt1"><code class="js spaces"> </code><code class="js plain">} </code><code class="js keyword">else</code> <code class="js plain">{</code></div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number20 index19 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number21 index20 alt2"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>总结</h2><p>通过上面的例子我们可以发现,OkHttp在很多时候使用都是很方便的,而且很多代码也有重复,因此特地整理了下面的工具类。<br>注意:</p><ul class=" list-paddingleft-2"><li><p>OkHttp官方文档并不建议我们创建多个OkHttpClient,因此全局使用一个。 如果有需要,可以使用clone方法,再进行自定义。这点在后面的高级教程里会提到。<br></p></li><li><p>enqueue为OkHttp提供的异步方法,入门教程中并没有提到,后面的高级教程里会有解释。</p></li></ul><div><div id="highlighter_544809" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div><div class="line number36 index35 alt1">36</div><div class="line number37 index36 alt2">37</div><div class="line number38 index37 alt1">38</div><div class="line number39 index38 alt2">39</div><div class="line number40 index39 alt1">40</div><div class="line number41 index40 alt2">41</div><div class="line number42 index41 alt1">42</div><div class="line number43 index42 alt2">43</div><div class="line number44 index43 alt1">44</div><div class="line number45 index44 alt2">45</div><div class="line number46 index45 alt1">46</div><div class="line number47 index46 alt2">47</div><div class="line number48 index47 alt1">48</div><div class="line number49 index48 alt2">49</div><div class="line number50 index49 alt1">50</div><div class="line number51 index50 alt2">51</div><div class="line number52 index51 alt1">52</div><div class="line number53 index52 alt2">53</div><div class="line number54 index53 alt1">54</div><div class="line number55 index54 alt2">55</div><div class="line number56 index55 alt1">56</div><div class="line number57 index56 alt2">57</div><div class="line number58 index57 alt1">58</div><div class="line number59 index58 alt2">59</div><div class="line number60 index59 alt1">60</div><div class="line number61 index60 alt2">61</div><div class="line number62 index61 alt1">62</div><div class="line number63 index62 alt2">63</div><div class="line number64 index63 alt1">64</div><div class="line number65 index64 alt2">65</div><div class="line number66 index65 alt1">66</div><div class="line number67 index66 alt2">67</div><div class="line number68 index67 alt1">68</div><div class="line number69 index68 alt2">69</div><div class="line number70 index69 alt1">70</div><div class="line number71 index70 alt2">71</div><div class="line number72 index71 alt1">72</div><div class="line number73 index72 alt2">73</div><div class="line number74 index73 alt1">74</div><div class="line number75 index74 alt2">75</div><div class="line number76 index75 alt1">76</div><div class="line number77 index76 alt2">77</div><div class="line number78 index77 alt1">78</div><div class="line number79 index78 alt2">79</div><div class="line number80 index79 alt1">80</div><div class="line number81 index80 alt2">81</div><div class="line number82 index81 alt1">82</div><div class="line number83 index82 alt2">83</div><div class="line number84 index83 alt1">84</div><div class="line number85 index84 alt2">85</div><div class="line number86 index85 alt1">86</div><div class="line number87 index86 alt2">87</div><div class="line number88 index87 alt1">88</div><div class="line number89 index88 alt2">89</div><div class="line number90 index89 alt1">90</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">import java.io.IOException;</code></div><div class="line number2 index1 alt1"><code class="js plain">import java.util.List;</code></div><div class="line number3 index2 alt2"><code class="js plain">import java.util.concurrent.TimeUnit;</code></div><div class="line number4 index3 alt1"><code class="js plain">import org.apache.http.client.utils.URLEncodedUtils;</code></div><div class="line number5 index4 alt2"><code class="js plain">import org.apache.http.message.BasicNameValuePair;</code></div><div class="line number6 index5 alt1"><code class="js plain">import cn.wiz.sdk.constant.WizConstant;</code></div><div class="line number7 index6 alt2"><code class="js plain">import com.squareup.okhttp.Callback;</code></div><div class="line number8 index7 alt1"><code class="js plain">import com.squareup.okhttp.OkHttpClient;</code></div><div class="line number9 index8 alt2"><code class="js plain">import com.squareup.okhttp.Request;</code></div><div class="line number10 index9 alt1"><code class="js plain">import com.squareup.okhttp.Response; </code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code> </div><div class="line number12 index11 alt1"><code class="js plain">public class OkHttpUtil {</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">private static final OkHttpClient mOkHttpClient = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">static{</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">mOkHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js comments">/**</code></div><div class="line number18 index17 alt1"><code class="js spaces"> </code><code class="js comments">* 该不会开启异步线程。</code></div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js comments">* @param request</code></div><div class="line number20 index19 alt1"><code class="js spaces"> </code><code class="js comments">* @return</code></div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js comments">* @throws IOException</code></div><div class="line number22 index21 alt1"><code class="js spaces"> </code><code class="js comments">*/</code></div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js plain">public static Response execute(Request request) throws IOException{</code></div><div class="line number24 index23 alt1"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">mOkHttpClient.newCall(request).execute();</code></div><div class="line number25 index24 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number26 index25 alt1"><code class="js spaces"> </code><code class="js comments">/**</code></div><div class="line number27 index26 alt2"><code class="js spaces"> </code><code class="js comments">* 开启异步线程访问网络</code></div><div class="line number28 index27 alt1"><code class="js spaces"> </code><code class="js comments">* @param request</code></div><div class="line number29 index28 alt2"><code class="js spaces"> </code><code class="js comments">* @param responseCallback</code></div><div class="line number30 index29 alt1"><code class="js spaces"> </code><code class="js comments">*/</code></div><div class="line number31 index30 alt2"><code class="js spaces"> </code><code class="js plain">public static void enqueue(Request request, Callback responseCallback){</code></div><div class="line number32 index31 alt1"><code class="js spaces"> </code><code class="js plain">mOkHttpClient.newCall(request).enqueue(responseCallback);</code></div><div class="line number33 index32 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number34 index33 alt1"><code class="js spaces"> </code><code class="js comments">/**</code></div><div class="line number35 index34 alt2"><code class="js spaces"> </code><code class="js comments">* 开启异步线程访问网络, 且不在意返回结果(实现空callback)</code></div><div class="line number36 index35 alt1"><code class="js spaces"> </code><code class="js comments">* @param request</code></div><div class="line number37 index36 alt2"><code class="js spaces"> </code><code class="js comments">*/</code></div><div class="line number38 index37 alt1"><code class="js spaces"> </code><code class="js plain">public static void enqueue(Request request){</code></div><div class="line number39 index38 alt2"><code class="js spaces"> </code><code class="js plain">mOkHttpClient.newCall(request).enqueue(</code><code class="js keyword">new</code> <code class="js plain">Callback() {</code></div><div class="line number40 index39 alt1"><code class="js spaces"> </code> </div><div class="line number41 index40 alt2"><code class="js spaces"> </code><code class="js plain">@Override</code></div><div class="line number42 index41 alt1"><code class="js spaces"> </code><code class="js plain">public void onResponse(Response arg0) throws IOException {</code></div><div class="line number43 index42 alt2"><code class="js spaces"> </code> </div><div class="line number44 index43 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number45 index44 alt2"><code class="js spaces"> </code> </div><div class="line number46 index45 alt1"><code class="js spaces"> </code><code class="js plain">@Override</code></div><div class="line number47 index46 alt2"><code class="js spaces"> </code><code class="js plain">public void onFailure(Request arg0, IOException arg1) {</code></div><div class="line number48 index47 alt1"><code class="js spaces"> </code> </div><div class="line number49 index48 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number50 index49 alt1"><code class="js spaces"> </code><code class="js plain">});</code></div><div class="line number51 index50 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number52 index51 alt1"><code class="js spaces"> </code><code class="js plain">public static String getStringFromServer(String url) throws IOException{</code></div><div class="line number53 index52 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder().url(url).build();</code></div><div class="line number54 index53 alt1"><code class="js spaces"> </code><code class="js plain">Response response = execute(request);</code></div><div class="line number55 index54 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(response.isSuccessful()) {</code></div><div class="line number56 index55 alt1"><code class="js spaces"> </code><code class="js plain">String responseUrl = response.body().string();</code></div><div class="line number57 index56 alt2"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">responseUrl;</code></div><div class="line number58 index57 alt1"><code class="js spaces"> </code><code class="js plain">} </code><code class="js keyword">else</code> <code class="js plain">{</code></div><div class="line number59 index58 alt2"><code class="js spaces"> </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number60 index59 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number61 index60 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number62 index61 alt1"><code class="js spaces"> </code><code class="js plain">private static final String CHARSET_NAME = </code><code class="js string">"UTF-8"</code><code class="js plain">;</code></div><div class="line number63 index62 alt2"><code class="js spaces"> </code><code class="js comments">/**</code></div><div class="line number64 index63 alt1"><code class="js spaces"> </code><code class="js comments">* 这里使用了HttpClinet的API。只是为了方便</code></div><div class="line number65 index64 alt2"><code class="js spaces"> </code><code class="js comments">* @param params</code></div><div class="line number66 index65 alt1"><code class="js spaces"> </code><code class="js comments">* @return</code></div><div class="line number67 index66 alt2"><code class="js spaces"> </code><code class="js comments">*/</code></div><div class="line number68 index67 alt1"><code class="js spaces"> </code><code class="js plain">public static String formatParams(List<BasicNameValuePair> params){</code></div><div class="line number69 index68 alt2"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">URLEncodedUtils.format(params, CHARSET_NAME);</code></div><div class="line number70 index69 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number71 index70 alt2"><code class="js spaces"> </code><code class="js comments">/**</code></div><div class="line number72 index71 alt1"><code class="js spaces"> </code><code class="js comments">* 为HttpGet 的 url 方便的添加多个name value 参数。</code></div><div class="line number73 index72 alt2"><code class="js spaces"> </code><code class="js comments">* @param url</code></div><div class="line number74 index73 alt1"><code class="js spaces"> </code><code class="js comments">* @param params</code></div><div class="line number75 index74 alt2"><code class="js spaces"> </code><code class="js comments">* @return</code></div><div class="line number76 index75 alt1"><code class="js spaces"> </code><code class="js comments">*/</code></div><div class="line number77 index76 alt2"><code class="js spaces"> </code><code class="js plain">public static String attachHttpGetParams(String url, List<BasicNameValuePair> params){</code></div><div class="line number78 index77 alt1"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">url + </code><code class="js string">"?"</code> <code class="js plain">+ formatParams(params);</code></div><div class="line number79 index78 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number80 index79 alt1"><code class="js spaces"> </code><code class="js comments">/**</code></div><div class="line number81 index80 alt2"><code class="js spaces"> </code><code class="js comments">* 为HttpGet 的 url 方便的添加1个name value 参数。</code></div><div class="line number82 index81 alt1"><code class="js spaces"> </code><code class="js comments">* @param url</code></div><div class="line number83 index82 alt2"><code class="js spaces"> </code><code class="js comments">* @param name</code></div><div class="line number84 index83 alt1"><code class="js spaces"> </code><code class="js comments">* @param value</code></div><div class="line number85 index84 alt2"><code class="js spaces"> </code><code class="js comments">* @return</code></div><div class="line number86 index85 alt1"><code class="js spaces"> </code><code class="js comments">*/</code></div><div class="line number87 index86 alt2"><code class="js spaces"> </code><code class="js plain">public static String attachHttpGetParam(String url, String name, String value){</code></div><div class="line number88 index87 alt1"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">url + </code><code class="js string">"?"</code> <code class="js plain">+ name + </code><code class="js string">"="</code> <code class="js plain">+ value;</code></div><div class="line number89 index88 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number90 index89 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h1>高级</h1><p>高级属性其实用的不多,这里主要是对OkHttp github官方教程进行了翻译。<br></p><h2>同步get</h2><p>下载一个文件,打印他的响应头,以string形式打印响应体。<br>响应体的 <code>string()</code> 方法对于小文档来说十分方便、高效。但是如果响应体太大(超过1MB),应避免适应 <code>string()</code>方法 ,因为他会将把整个文档加载到内存中。<br>对于超过1MB的响应body,应使用流的方式来处理body。<a href="http://www.cnblogs.com/ct2011/p/3997368.html"></a></p><div><div id="highlighter_613263" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="js plain">public void run() throws Exception {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="http://publicobject.com/helloworld.txt">http://publicobject.com/helloworld.txt</a>"</code><code class="js plain">)</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number7 index6 alt2"> </div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">Headers responseHeaders = response.headers();</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js keyword">for</code> <code class="js plain">(int i = 0; i < responseHeaders.size(); i++) {</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(responseHeaders.name(i) + </code><code class="js string">": "</code> <code class="js plain">+ responseHeaders.value(i));</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number15 index14 alt2"> </div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(response.body().string());</code></div><div class="line number17 index16 alt2"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><p><a href="http://www.cnblogs.com/ct2011/p/3997368.html"></a></p><h2>异步get</h2><p>在一个工作线程中下载文件,当响应可读时回调Callback接口。读取响应时会阻塞当前线程。OkHttp现阶段不提供异步api来接收响应体。</p><div><div id="highlighter_828037" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="js plain">public void run() throws Exception {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="http://publicobject.com/helloworld.txt">http://publicobject.com/helloworld.txt</a>"</code><code class="js plain">)</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number7 index6 alt2"> </div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">client.newCall(request).enqueue(</code><code class="js keyword">new</code> <code class="js plain">Callback() {</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">@Override public void onFailure(Request request, Throwable throwable) {</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">throwable.printStackTrace();</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number12 index11 alt1"> </div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">@Override public void onResponse(Response response) throws IOException {</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number15 index14 alt2"> </div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">Headers responseHeaders = response.headers();</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js keyword">for</code> <code class="js plain">(int i = 0; i < responseHeaders.size(); i++) {</code></div><div class="line number18 index17 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(responseHeaders.name(i) + </code><code class="js string">": "</code> <code class="js plain">+ responseHeaders.value(i));</code></div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number20 index19 alt1"> </div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(response.body().string());</code></div><div class="line number22 index21 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js plain">});</code></div><div class="line number24 index23 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>提取响应头</h2><p>典型的HTTP头 像是一个 <code>Map<String, String></code> :每个字段都有一个或没有值。但是一些头允许多个值,像Guava的<a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html">Multimap</a>。例如:HTTP响应里面提供的<code>Vary</code>响应头,就是多值的。OkHttp的api试图让这些情况都适用。<br>当写请求头的时候,使用<code>header(name, value)</code>可以设置唯一的name、value。如果已经有值,旧的将被移除,然后添加新的。使用<code>addHeader(name, value)</code>可以添加多值(添加,不移除已有的)。<br>当读取响应头时,使用<code>header(name)</code>返回最后出现的name、value。通常情况这也是唯一的name、value。如果没有值,那么<code>header(name)</code>将返回null。如果想读取字段对应的所有值,使用<code>headers(name)</code>会返回一个list。<br>为了获取所有的Header,Headers类支持按index访问。</p><div><div id="highlighter_716956" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="js plain">public void run() throws Exception {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="https://api.github.com/repos/square/okhttp/issues">https://api.github.com/repos/square/okhttp/issues</a>"</code><code class="js plain">)</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">.header(</code><code class="js string">"User-Agent"</code><code class="js plain">, </code><code class="js string">"OkHttp Headers.java"</code><code class="js plain">)</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">.addHeader(</code><code class="js string">"Accept"</code><code class="js plain">, </code><code class="js string">"application/json; q=0.5"</code><code class="js plain">)</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">.addHeader(</code><code class="js string">"Accept"</code><code class="js plain">, </code><code class="js string">"application/vnd.github.v3+json"</code><code class="js plain">)</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number13 index12 alt2"> </div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Server: "</code> <code class="js plain">+ response.header(</code><code class="js string">"Server"</code><code class="js plain">));</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Date: "</code> <code class="js plain">+ response.header(</code><code class="js string">"Date"</code><code class="js plain">));</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Vary: "</code> <code class="js plain">+ response.headers(</code><code class="js string">"Vary"</code><code class="js plain">));</code></div><div class="line number17 index16 alt2"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>Post方式提交String</h2><p>使用HTTP POST提交请求到服务。这个例子提交了一个markdown文档到web服务,以HTML方式渲染markdown。因为整个请求体都在内存中,因此避免使用此api提交大文档(大于1MB)。</p><div><div id="highlighter_280488" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">public static final MediaType MEDIA_TYPE_MARKDOWN</code></div><div class="line number2 index1 alt1"><code class="js spaces"> </code><code class="js plain">= MediaType.parse(</code><code class="js string">"text/x-markdown; charset=utf-8"</code><code class="js plain">);</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="js plain">public void run() throws Exception {</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">String postBody = </code><code class="js string">""</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">+ </code><code class="js string">"Releases\n"</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">+ </code><code class="js string">"--------\n"</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">+ </code><code class="js string">"\n"</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">+ </code><code class="js string">" * _1.0_ May 6, 2013\n"</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">+ </code><code class="js string">" * _1.1_ June 15, 2013\n"</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">+ </code><code class="js string">" * _1.2_ August 11, 2013\n"</code><code class="js plain">;</code></div><div class="line number14 index13 alt1"> </div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="https://api.github.com/markdown/raw">https://api.github.com/markdown/raw</a>"</code><code class="js plain">)</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js plain">.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))</code></div><div class="line number18 index17 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number19 index18 alt2"> </div><div class="line number20 index19 alt1"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number22 index21 alt1"> </div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(response.body().string());</code></div><div class="line number24 index23 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>Post方式提交流</h2><p>以流的方式POST提交请求体。请求体的内容由流写入产生。这个例子是流直接写入<a href="https://github.com/square/okio">Okio</a>的BufferedSink。你的程序可能会使用<code>OutputStream</code>,你可以使用<code>BufferedSink.outputStream()</code>来获取。</p><div><div id="highlighter_546966" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div><div class="line number36 index35 alt1">36</div><div class="line number37 index36 alt2">37</div><div class="line number38 index37 alt1">38</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">public static final MediaType MEDIA_TYPE_MARKDOWN</code></div><div class="line number2 index1 alt1"><code class="js spaces"> </code><code class="js plain">= MediaType.parse(</code><code class="js string">"text/x-markdown; charset=utf-8"</code><code class="js plain">);</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="js plain">public void run() throws Exception {</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">RequestBody requestBody = </code><code class="js keyword">new</code> <code class="js plain">RequestBody() {</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">@Override public MediaType contentType() {</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">MEDIA_TYPE_MARKDOWN;</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number11 index10 alt2"> </div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">@Override public void writeTo(BufferedSink sink) throws IOException {</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">sink.writeUtf8(</code><code class="js string">"Numbers\n"</code><code class="js plain">);</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">sink.writeUtf8(</code><code class="js string">"-------\n"</code><code class="js plain">);</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js keyword">for</code> <code class="js plain">(int i = 2; i <= 997; i++) {</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">sink.writeUtf8(String.format(</code><code class="js string">" * %s = %s\n"</code><code class="js plain">, i, factor(i)));</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number18 index17 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number19 index18 alt2"> </div><div class="line number20 index19 alt1"><code class="js spaces"> </code><code class="js plain">private String factor(int n) {</code></div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js keyword">for</code> <code class="js plain">(int i = 2; i < n; i++) {</code></div><div class="line number22 index21 alt1"><code class="js spaces"> </code><code class="js plain">int x = n / i;</code></div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(x * i == n) </code><code class="js keyword">return</code> <code class="js plain">factor(x) + </code><code class="js string">" × "</code> <code class="js plain">+ i;</code></div><div class="line number24 index23 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number25 index24 alt2"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">Integer.toString(n);</code></div><div class="line number26 index25 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number27 index26 alt2"><code class="js spaces"> </code><code class="js plain">};</code></div><div class="line number28 index27 alt1"> </div><div class="line number29 index28 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number30 index29 alt1"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="https://api.github.com/markdown/raw">https://api.github.com/markdown/raw</a>"</code><code class="js plain">)</code></div><div class="line number31 index30 alt2"><code class="js spaces"> </code><code class="js plain">.post(requestBody)</code></div><div class="line number32 index31 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number33 index32 alt2"> </div><div class="line number34 index33 alt1"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number35 index34 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number36 index35 alt1"> </div><div class="line number37 index36 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(response.body().string());</code></div><div class="line number38 index37 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>Post方式提交文件</h2><p>以文件作为请求体是十分简单的。</p><div><div id="highlighter_632828" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">public static final MediaType MEDIA_TYPE_MARKDOWN</code></div><div class="line number2 index1 alt1"><code class="js spaces"> </code><code class="js plain">= MediaType.parse(</code><code class="js string">"text/x-markdown; charset=utf-8"</code><code class="js plain">);</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="js plain">public void run() throws Exception {</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">File file = </code><code class="js keyword">new</code> <code class="js plain">File(</code><code class="js string">"README.md"</code><code class="js plain">);</code></div><div class="line number8 index7 alt1"> </div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="https://api.github.com/markdown/raw">https://api.github.com/markdown/raw</a>"</code><code class="js plain">)</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number13 index12 alt2"> </div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number16 index15 alt1"> </div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(response.body().string());</code></div><div class="line number18 index17 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>Post方式提交表单</h2><p>使用<code>FormEncodingBuilder</code>来构建和HTML<code><form></code>标签相同效果的请求体。键值对将使用一种HTML兼容形式的URL编码来进行编码。</p><div><div id="highlighter_260504" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="js plain">public void run() throws Exception {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">RequestBody formBody = </code><code class="js keyword">new</code> <code class="js plain">FormEncodingBuilder()</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">.add(</code><code class="js string">"search"</code><code class="js plain">, </code><code class="js string">"Jurassic Park"</code><code class="js plain">)</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="https://en.wikipedia.org/w/index.php">https://en.wikipedia.org/w/index.php</a>"</code><code class="js plain">)</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">.post(formBody)</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number11 index10 alt2"> </div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number14 index13 alt1"> </div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(response.body().string());</code></div><div class="line number16 index15 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>Post方式提交分块请求</h2><p><code>MultipartBuilder</code>可以构建复杂的请求体,与HTML文件上传形式兼容。多块请求体中每块请求都是一个请求体,可以定义自己的请求头。这些请求头可以用来描述这块请求,例如他的<code>Content-Disposition</code>。如果<code>Content-Length</code>和<code>Content-Type</code>可用的话,他们会被自动添加到请求头中。</p><div><div id="highlighter_91012" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private static final String IMGUR_CLIENT_ID = </code><code class="js string">"..."</code><code class="js plain">;</code></div><div class="line number2 index1 alt1"><code class="js plain">private static final MediaType MEDIA_TYPE_PNG = MediaType.parse(</code><code class="js string">"image/png"</code><code class="js plain">);</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="js plain">public void run() throws Exception {</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js comments">// Use the imgur image upload API as documented at <a href="https://api.imgur.com/endpoints/image">https://api.imgur.com/endpoints/image</a></code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">RequestBody requestBody = </code><code class="js keyword">new</code> <code class="js plain">MultipartBuilder()</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">.type(MultipartBuilder.FORM)</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">.addPart(</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">Headers.of(</code><code class="js string">"Content-Disposition"</code><code class="js plain">, </code><code class="js string">"form-data; name=\"title\""</code><code class="js plain">),</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">RequestBody.create(</code><code class="js keyword">null</code><code class="js plain">, </code><code class="js string">"Square Logo"</code><code class="js plain">))</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">.addPart(</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">Headers.of(</code><code class="js string">"Content-Disposition"</code><code class="js plain">, </code><code class="js string">"form-data; name=\"image\""</code><code class="js plain">),</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">RequestBody.create(MEDIA_TYPE_PNG, </code><code class="js keyword">new</code> <code class="js plain">File(</code><code class="js string">"website/static/logo-square.png"</code><code class="js plain">)))</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number17 index16 alt2"> </div><div class="line number18 index17 alt1"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js plain">.header(</code><code class="js string">"Authorization"</code><code class="js plain">, </code><code class="js string">"Client-ID "</code> <code class="js plain">+ IMGUR_CLIENT_ID)</code></div><div class="line number20 index19 alt1"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="https://api.imgur.com/3/image">https://api.imgur.com/3/image</a>"</code><code class="js plain">)</code></div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js plain">.post(requestBody)</code></div><div class="line number22 index21 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number23 index22 alt2"> </div><div class="line number24 index23 alt1"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number25 index24 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number26 index25 alt1"> </div><div class="line number27 index26 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(response.body().string());</code></div><div class="line number28 index27 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>使用Gson来解析JSON响应</h2><p>Gson是一个在JSON和Java对象之间转换非常方便的api。这里我们用Gson来解析Github API的JSON响应。<br>注意:<code>ResponseBody.charStream()</code>使用响应头<code>Content-Type</code>指定的字符集来解析响应体。默认是UTF-8。</p><div><div id="highlighter_680583" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"><code class="js plain">private final Gson gson = </code><code class="js keyword">new</code> <code class="js plain">Gson();</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="js plain">public void run() throws Exception {</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="https://api.github.com/gists/c2a7c39532239ff261be">https://api.github.com/gists/c2a7c39532239ff261be</a>"</code><code class="js plain">)</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">Gist gist = gson.fromJson(response.body().charStream(), Gist.class);</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js keyword">for</code> <code class="js plain">(Map.Entry<String, GistFile> entry : gist.files.entrySet()) {</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(entry.getKey());</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(entry.getValue().content);</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number16 index15 alt1"><code class="js plain">}</code></div><div class="line number17 index16 alt2"> </div><div class="line number18 index17 alt1"><code class="js plain">static class Gist {</code></div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js plain">Map<String, GistFile> files;</code></div><div class="line number20 index19 alt1"><code class="js plain">}</code></div><div class="line number21 index20 alt2"> </div><div class="line number22 index21 alt1"><code class="js plain">static class GistFile {</code></div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js plain">String content;</code></div><div class="line number24 index23 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>响应缓存</h2><p>为了缓存响应,你需要一个你可以读写的缓存目录,和缓存大小的限制。这个缓存目录应该是私有的,不信任的程序应不能读取缓存内容。<br>一个缓存目录同时拥有多个缓存访问是错误的。大多数程序只需要调用一次<code>new OkHttp()</code>,在第一次调用时配置好缓存,然后其他地方只需要调用这个实例就可以了。否则两个缓存示例互相干扰,破坏响应缓存,而且有可能会导致程序崩溃。<br>响应缓存使用HTTP头作为配置。你可以在请求头中添加<code>Cache-Control: max-stale=3600</code> ,OkHttp缓存会支持。你的服务通过响应头确定响应缓存多长时间,例如使用<code>Cache-Control: max-age=9600</code>。</p><div><div id="highlighter_443848" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final OkHttpClient client;</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="js plain">public CacheResponse(File cacheDirectory) throws Exception {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">int cacheSize = 10 * 1024 * 1024; </code><code class="js comments">// 10 MiB</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">Cache cache = </code><code class="js keyword">new</code> <code class="js plain">Cache(cacheDirectory, cacheSize);</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">client.setCache(cache);</code></div><div class="line number9 index8 alt2"><code class="js plain">}</code></div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"><code class="js plain">public void run() throws Exception {</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="http://publicobject.com/helloworld.txt">http://publicobject.com/helloworld.txt</a>"</code><code class="js plain">)</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number15 index14 alt2"> </div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">Response response1 = client.newCall(request).execute();</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response1.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response1);</code></div><div class="line number18 index17 alt1"> </div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js plain">String response1Body = response1.body().string();</code></div><div class="line number20 index19 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 1 response: "</code> <code class="js plain">+ response1);</code></div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 1 cache response: "</code> <code class="js plain">+ response1.cacheResponse());</code></div><div class="line number22 index21 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 1 network response: "</code> <code class="js plain">+ response1.networkResponse());</code></div><div class="line number23 index22 alt2"> </div><div class="line number24 index23 alt1"><code class="js spaces"> </code><code class="js plain">Response response2 = client.newCall(request).execute();</code></div><div class="line number25 index24 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response2.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response2);</code></div><div class="line number26 index25 alt1"> </div><div class="line number27 index26 alt2"><code class="js spaces"> </code><code class="js plain">String response2Body = response2.body().string();</code></div><div class="line number28 index27 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 2 response: "</code> <code class="js plain">+ response2);</code></div><div class="line number29 index28 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 2 cache response: "</code> <code class="js plain">+ response2.cacheResponse());</code></div><div class="line number30 index29 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 2 network response: "</code> <code class="js plain">+ response2.networkResponse());</code></div><div class="line number31 index30 alt2"> </div><div class="line number32 index31 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 2 equals Response 1? "</code> <code class="js plain">+ response1Body.equals(response2Body));</code></div><div class="line number33 index32 alt2"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>扩展</h2><p>在这一节还提到了下面一句:<br>There are cache headers to force a cached response,
force a network response, or force the network response to be validated
with a conditional GET.</p><p>我不是很懂cache,平时用到的也不多,所以把Google在Android Developers一段相关的解析放到这里吧。</p><h3 id="force-a-network-response">Force a Network Response</h3><p>In some situations, such as after a user clicks a 'refresh' button,
it may be necessary to skip the cache, and fetch data directly from the
server. To force a full refresh, add the no-cache directive:</p><pre class="java">connection.addRequestProperty("Cache-Control", "no-cache");</pre><p>If it is only necessary to force a cached response to be validated by the server, use the more efficient max-age=0 instead:</p><pre class="java">connection.addRequestProperty("Cache-Control", "max-age=0");</pre><h3 id="force-a-cache-response">Force a Cache Response</h3><p>Sometimes you'll want to show resources if they are available
immediately, but not otherwise. This can be used so your application can
show something while waiting for the latest data to be downloaded. To
restrict a request to locally-cached resources, add the only-if-cached
directive:</p><div><div id="highlighter_710257" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js keyword">try</code> <code class="js plain">{</code></div><div class="line number2 index1 alt1"><code class="js spaces"> </code><code class="js plain">connection.addRequestProperty(</code><code class="js string">"Cache-Control"</code><code class="js plain">, </code><code class="js string">"only-if-cached"</code><code class="js plain">);</code></div><div class="line number3 index2 alt2"><code class="js spaces"> </code><code class="js plain">InputStream cached = connection.getInputStream();</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js comments">// the resource was cached! show it</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js keyword">catch</code> <code class="js plain">(FileNotFoundException e) {</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js comments">// the resource was not cached</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number8 index7 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><p>This technique works even better in situations where a stale response is
better than no response. To permit stale cached responses, use the
max-stale directive with the maximum staleness in seconds:</p><div><div id="highlighter_392790" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">int maxStale = 60 * 60 * 24 * 28; </code><code class="js comments">// tolerate 4-weeks staleconnection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);</code></div></div></td></tr></tbody></table></div></div><p>以上信息来自:<a href="http://developer.android.com/reference/android/net/http/HttpResponseCache.html">HttpResponseCache - Android SDK | Android Developers</a></p><h2>取消一个Call</h2><p>使用<code>Call.cancel()</code>可以立即停止掉一个正在执行的call。如果一个线程正在写请求或者读响应,将会引发<code>IOException</code>。当call没有必要的时候,使用这个api可以节约网络资源。例如当用户离开一个应用时。不管同步还是异步的call都可以取消。<br>你可以通过tags来同时取消多个请求。当你构建一请求时,使用<code>RequestBuilder.tag(tag)</code>来分配一个标签。之后你就可以用<code>OkHttpClient.cancel(tag)</code>来取消所有带有这个tag的call。</p><div><div id="highlighter_586672" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);</code></div><div class="line number2 index1 alt1"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="js plain">public void run() throws Exception {</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="http://httpbin.org/delay/2">http://httpbin.org/delay/2</a>"</code><code class="js plain">) // This URL is served </code><code class="js keyword">with</code> <code class="js plain">a 2 second delay.</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number8 index7 alt1"> </div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">final long startNanos = System.nanoTime();</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">final Call call = client.newCall(request);</code></div><div class="line number11 index10 alt2"> </div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js comments">// Schedule a job to cancel the call in 1 second.</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">executor.schedule(</code><code class="js keyword">new</code> <code class="js plain">Runnable() {</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">@Override public void run() {</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f Canceling call.%n"</code><code class="js plain">, (System.nanoTime() - startNanos) / 1e9f);</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">call.cancel();</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f Canceled call.%n"</code><code class="js plain">, (System.nanoTime() - startNanos) / 1e9f);</code></div><div class="line number18 index17 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js plain">}, 1, TimeUnit.SECONDS);</code></div><div class="line number20 index19 alt1"> </div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js keyword">try</code> <code class="js plain">{</code></div><div class="line number22 index21 alt1"><code class="js spaces"> </code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f Executing call.%n"</code><code class="js plain">, (System.nanoTime() - startNanos) / 1e9f);</code></div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js plain">Response response = call.execute();</code></div><div class="line number24 index23 alt1"><code class="js spaces"> </code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f Call was expected to fail, but completed: %s%n"</code><code class="js plain">,</code></div><div class="line number25 index24 alt2"><code class="js spaces"> </code><code class="js plain">(System.nanoTime() - startNanos) / 1e9f, response);</code></div><div class="line number26 index25 alt1"><code class="js spaces"> </code><code class="js plain">} </code><code class="js keyword">catch</code> <code class="js plain">(IOException e) {</code></div><div class="line number27 index26 alt2"><code class="js spaces"> </code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f Call failed as expected: %s%n"</code><code class="js plain">,</code></div><div class="line number28 index27 alt1"><code class="js spaces"> </code><code class="js plain">(System.nanoTime() - startNanos) / 1e9f, e);</code></div><div class="line number29 index28 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number30 index29 alt1"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>超时</h2><p>没有响应时使用超时结束call。没有响应的原因可能是客户点链接问题、服务器可用性问题或者这之间的其他东西。OkHttp支持连接,读取和写入超时。</p><div><div id="highlighter_302060" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final OkHttpClient client;</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="js plain">public ConfigureTimeouts() throws Exception {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">client.setConnectTimeout(10, TimeUnit.SECONDS);</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">client.setWriteTimeout(10, TimeUnit.SECONDS);</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">client.setReadTimeout(30, TimeUnit.SECONDS);</code></div><div class="line number8 index7 alt1"><code class="js plain">}</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"><code class="js plain">public void run() throws Exception {</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="http://httpbin.org/delay/2">http://httpbin.org/delay/2</a>"</code><code class="js plain">) // This URL is served </code><code class="js keyword">with</code> <code class="js plain">a 2 second delay.</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number14 index13 alt1"> </div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response completed: "</code> <code class="js plain">+ response);</code></div><div class="line number17 index16 alt2"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>每个call的配置</h2><p>使用<code>OkHttpClient</code>,所有的HTTP Client配置包括代理设置、超时设置、缓存设置。当你需要为单个call改变配置的时候,clone 一个 <code>OkHttpClient</code>。这个api将会返回一个浅拷贝(shallow copy),你可以用来单独自定义。下面的例子中,我们让一个请求是500ms的超时、另一个是3000ms的超时。</p><div><div id="highlighter_384340" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="js plain">public void run() throws Exception {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="http://httpbin.org/delay/1">http://httpbin.org/delay/1</a>"</code><code class="js plain">) // This URL is served </code><code class="js keyword">with</code> <code class="js plain">a 1 second delay.</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number7 index6 alt2"> </div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js keyword">try</code> <code class="js plain">{</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">Response response = client.clone() </code><code class="js comments">// Clone to make a customized OkHttp for this request.</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">.setReadTimeout(500, TimeUnit.MILLISECONDS)</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">.newCall(request)</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">.execute();</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 1 succeeded: "</code> <code class="js plain">+ response);</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">} </code><code class="js keyword">catch</code> <code class="js plain">(IOException e) {</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 1 failed: "</code> <code class="js plain">+ e);</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number17 index16 alt2"> </div><div class="line number18 index17 alt1"><code class="js spaces"> </code><code class="js keyword">try</code> <code class="js plain">{</code></div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js plain">Response response = client.clone() </code><code class="js comments">// Clone to make a customized OkHttp for this request.</code></div><div class="line number20 index19 alt1"><code class="js spaces"> </code><code class="js plain">.setReadTimeout(3000, TimeUnit.MILLISECONDS)</code></div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js plain">.newCall(request)</code></div><div class="line number22 index21 alt1"><code class="js spaces"> </code><code class="js plain">.execute();</code></div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 2 succeeded: "</code> <code class="js plain">+ response);</code></div><div class="line number24 index23 alt1"><code class="js spaces"> </code><code class="js plain">} </code><code class="js keyword">catch</code> <code class="js plain">(IOException e) {</code></div><div class="line number25 index24 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Response 2 failed: "</code> <code class="js plain">+ e);</code></div><div class="line number26 index25 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number27 index26 alt2"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><h2>处理验证</h2><p>这部分和HTTP AUTH有关。<br>相关资料:<a href="http://blog.csdn.net/wwwsq/article/details/7255062">HTTP AUTH 那些事 - 王绍全的博客 - 博客频道 - CSDN.NET</a></p><p>OkHttp会自动重试未验证的请求。当响应是<code>401 Not Authorized</code>时,<code>Authenticator</code>会被要求提供证书。Authenticator的实现中需要建立一个新的包含证书的请求。如果没有证书可用,返回null来跳过尝试。</p><div><div id="highlighter_968965" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">public List<Challenge> challenges()</code></div><div class="line number2 index1 alt1"><code class="js plain">Returns the authorization challenges appropriate </code><code class="js keyword">for</code> <code class="js keyword">this</code> <code class="js plain">response's code. </code></div><div class="line number3 index2 alt2"><code class="js plain">If the response code is 401 unauthorized, </code></div><div class="line number4 index3 alt1"><code class="js keyword">this</code> <code class="js plain">returns the </code><code class="js string">"WWW-Authenticate"</code> <code class="js plain">challenges.</code></div><div class="line number5 index4 alt2"><code class="js plain">If the response code is 407 proxy unauthorized, </code><code class="js keyword">this</code> <code class="js plain">returns the </code><code class="js string">"Proxy-Authenticate"</code> <code class="js plain">challenges.</code></div><div class="line number6 index5 alt1"><code class="js plain">Otherwise </code><code class="js keyword">this</code> <code class="js plain">returns an empty list of challenges.</code></div></div></td></tr></tbody></table></div></div><p>当需要实现一个<code>Basic</code> challenge, 使用<code>Credentials.basic(username, password)</code>来编码请求头。</p><div><div id="highlighter_969377" class="syntaxhighlighter js"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">private final OkHttpClient client = </code><code class="js keyword">new</code> <code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="js plain">public void run() throws Exception {</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">client.setAuthenticator(</code><code class="js keyword">new</code> <code class="js plain">Authenticator() {</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js plain">@Override public Request authenticate(Proxy proxy, Response response) {</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Authenticating for response: "</code> <code class="js plain">+ response);</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js plain">System.out.println(</code><code class="js string">"Challenges: "</code> <code class="js plain">+ response.challenges());</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">String credential = Credentials.basic(</code><code class="js string">"jesse"</code><code class="js plain">, </code><code class="js string">"password1"</code><code class="js plain">);</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js plain">response.request().newBuilder()</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">.header(</code><code class="js string">"Authorization"</code><code class="js plain">, credential)</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number13 index12 alt2"> </div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js plain">@Override public Request authenticateProxy(Proxy proxy, Response response) {</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js keyword">null</code><code class="js plain">; </code><code class="js comments">// Null indicates no attempt to authenticate.</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js plain">});</code></div><div class="line number18 index17 alt1"> </div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js plain">Request request = </code><code class="js keyword">new</code> <code class="js plain">Request.Builder()</code></div><div class="line number20 index19 alt1"><code class="js spaces"> </code><code class="js plain">.url(</code><code class="js string">"<a href="http://publicobject.com/secrets/hellosecret.txt">http://publicobject.com/secrets/hellosecret.txt</a>"</code><code class="js plain">)</code></div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js plain">.build();</code></div><div class="line number22 index21 alt1"> </div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js plain">Response response = client.newCall(request).execute();</code></div><div class="line number24 index23 alt1"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">(!response.isSuccessful()) </code><code class="js keyword">throw</code> <code class="js keyword">new</code> <code class="js plain">IOException(</code><code class="js string">"Unexpected code "</code> <code class="js plain">+ response);</code></div><div class="line number25 index24 alt2"> </div><div class="line number26 index25 alt1"><code class="js spaces"> </code><code class="js plain">System.out.println(response.body().string());</code></div><div class="line number27 index26 alt2"><code class="js plain">}</code></div></div></td></tr></tbody></table></div></div><p><br></p><p>转自 <a textvalue="OkHttp使用介绍" target="_self" href="http://www.cnblogs.com/ct2011/p/4001708.html">OkHttp使用介绍</a> 同时整合了 <a textvalue="OkHttp使用进阶" target="_self" href="http://www.cnblogs.com/ct2011/p/3997368.html">OkHttp使用进阶</a> 这篇文章以及其中的评论。</p>
- Introduction
- 爬虫相关技能介绍
- 爬虫简单介绍
- 爬虫涉及到的知识点
- 爬虫用途
- 爬虫流程介绍
- 需求描述
- Http请求处理
- http基础知识介绍
- http状态码
- httpheader
- java原生态处理http
- URL类
- 获取URL请求状态
- 模拟Http请求
- apache httpclient
- Httpclient1
- httpclient2
- httpclient3
- httpclient4
- httpclient5
- httpclient6
- okhttp
- OKhttp使用教程
- 技术使用
- java执行javascript
- 网页解析
- Xpath介绍
- HtmlCleaner
- HtmlCleaner介绍
- HtmlCleaner使用
- HtmlParser
- HtmlParser介绍
- Jsoup
- 解析和遍历一个HTML文档
- 解析一个HTML字符串
- 解析一个body片断
- 从一个URL加载一个Document
- 从一个文件加载一个文档
- 使用DOM方法来遍历一个文档
- 使用选择器语法来查找元素
- 从元素抽取属性,文本和HTML
- 处理URLs
- 示例程序 获取所有链接
- 设置属性的值
- 设置一个元素的HTML内容
- 消除不受信任的HTML (来防止XSS攻击)
- 正则表达式
- elasticsearch笔记
- 下载安装elasticsearch
- 检查es服务健康