🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# OkHttp使用教程 <p>Android系统提供了两种HTTP通信类,HttpURLConnection和HttpClient。<br><a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">关于HttpURLConnection和HttpClient的选择&gt;&gt;官方博客</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。如果你用了&nbsp;Apache&nbsp;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&amp;g=com.squareup.okhttp&amp;a=okhttp&amp;v=LATEST">OkHttp</a><br><a href="http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&amp;g=com.squareup.okio&amp;a=okio&amp;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&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="js plain">String&nbsp;run(String&nbsp;url)&nbsp;throws&nbsp;IOException&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder().url(url).build();</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(response.isSuccessful())&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">response.body().string();</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}&nbsp;</code><code class="js keyword">else</code>&nbsp;<code class="js plain">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;boolean&nbsp;isSuccessful()</code></div><div class="line number2 index1 alt1"><code class="js plain">Returns&nbsp;</code><code class="js keyword">true</code>&nbsp;<code class="js keyword">if</code>&nbsp;<code class="js plain">the&nbsp;code&nbsp;is&nbsp;</code><code class="js keyword">in</code>&nbsp;<code class="js plain">[200..300),</code></div><div class="line number3 index2 alt2"><code class="js spaces">&nbsp;</code><code class="js plain">which&nbsp;means&nbsp;the&nbsp;request&nbsp;was&nbsp;successfully&nbsp;received,&nbsp;understood,&nbsp;and&nbsp;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&nbsp;final&nbsp;String&nbsp;string()&nbsp;throws&nbsp;IOException</code></div><div class="line number2 index1 alt1"><code class="js plain">Returns&nbsp;the&nbsp;response&nbsp;as&nbsp;a&nbsp;string&nbsp;decoded&nbsp;</code><code class="js keyword">with</code>&nbsp;<code class="js plain">the&nbsp;charset&nbsp;of&nbsp;the&nbsp;Content-Type&nbsp;header.&nbsp;If&nbsp;that&nbsp;header&nbsp;is&nbsp;either&nbsp;absent&nbsp;or&nbsp;lacks&nbsp;a&nbsp;charset,</code></div><div class="line number3 index2 alt2"><code class="js spaces">&nbsp;</code><code class="js keyword">this</code>&nbsp;<code class="js plain">will&nbsp;attempt&nbsp;to&nbsp;decode&nbsp;the&nbsp;response&nbsp;body&nbsp;as&nbsp;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&nbsp;final&nbsp;InputStream&nbsp;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&nbsp;static&nbsp;final&nbsp;MediaType&nbsp;JSON&nbsp;=&nbsp;MediaType.parse(</code><code class="js string">"application/json;&nbsp;charset=utf-8"</code><code class="js plain">);</code></div><div class="line number2 index1 alt1"><code class="js plain">OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number3 index2 alt2"><code class="js plain">String&nbsp;post(String&nbsp;url,&nbsp;String&nbsp;json)&nbsp;throws&nbsp;IOException&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">RequestBody&nbsp;body&nbsp;=&nbsp;RequestBody.create(JSON,&nbsp;json);</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.url(url)</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.post(body)</code></div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">f&nbsp;(response.isSuccessful())&nbsp;{</code></div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">response.body().string();</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}&nbsp;</code><code class="js keyword">else</code>&nbsp;<code class="js plain">{</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"><code class="js plain">String&nbsp;post(String&nbsp;url,&nbsp;String&nbsp;json)&nbsp;throws&nbsp;IOException&nbsp;{</code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">RequestBody&nbsp;formBody&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">FormEncodingBuilder()</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.add(</code><code class="js string">"platform"</code><code class="js plain">,&nbsp;</code><code class="js string">"android"</code><code class="js plain">)</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.add(</code><code class="js string">"name"</code><code class="js plain">,&nbsp;</code><code class="js string">"bug"</code><code class="js plain">)</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.add(</code><code class="js string">"subject"</code><code class="js plain">,&nbsp;</code><code class="js string">"XXXXXXXXXXXXXXX"</code><code class="js plain">)</code></div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number9 index8 alt2">&nbsp;</div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.url(url)</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.post(body)</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number14 index13 alt1">&nbsp;</div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(response.isSuccessful())&nbsp;{</code></div><div class="line number17 index16 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">response.body().string();</code></div><div class="line number18 index17 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}&nbsp;</code><code class="js keyword">else</code>&nbsp;<code class="js plain">{</code></div><div class="line number19 index18 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number20 index19 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;java.io.IOException;</code></div><div class="line number2 index1 alt1"><code class="js plain">import&nbsp;java.util.List;</code></div><div class="line number3 index2 alt2"><code class="js plain">import&nbsp;java.util.concurrent.TimeUnit;</code></div><div class="line number4 index3 alt1"><code class="js plain">import&nbsp;org.apache.http.client.utils.URLEncodedUtils;</code></div><div class="line number5 index4 alt2"><code class="js plain">import&nbsp;org.apache.http.message.BasicNameValuePair;</code></div><div class="line number6 index5 alt1"><code class="js plain">import&nbsp;cn.wiz.sdk.constant.WizConstant;</code></div><div class="line number7 index6 alt2"><code class="js plain">import&nbsp;com.squareup.okhttp.Callback;</code></div><div class="line number8 index7 alt1"><code class="js plain">import&nbsp;com.squareup.okhttp.OkHttpClient;</code></div><div class="line number9 index8 alt2"><code class="js plain">import&nbsp;com.squareup.okhttp.Request;</code></div><div class="line number10 index9 alt1"><code class="js plain">import&nbsp;com.squareup.okhttp.Response;&nbsp;</code></div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;</code>&nbsp;</div><div class="line number12 index11 alt1"><code class="js plain">public&nbsp;class&nbsp;OkHttpUtil&nbsp;{</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">private&nbsp;static&nbsp;final&nbsp;OkHttpClient&nbsp;mOkHttpClient&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">static{</code></div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">mOkHttpClient.setConnectTimeout(30,&nbsp;TimeUnit.SECONDS);</code></div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number17 index16 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">/**</code></div><div class="line number18 index17 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;该不会开启异步线程。</code></div><div class="line number19 index18 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;request</code></div><div class="line number20 index19 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@return</code></div><div class="line number21 index20 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@throws&nbsp;IOException</code></div><div class="line number22 index21 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*/</code></div><div class="line number23 index22 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">public&nbsp;static&nbsp;Response&nbsp;execute(Request&nbsp;request)&nbsp;throws&nbsp;IOException{</code></div><div class="line number24 index23 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">mOkHttpClient.newCall(request).execute();</code></div><div class="line number25 index24 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number26 index25 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">/**</code></div><div class="line number27 index26 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;开启异步线程访问网络</code></div><div class="line number28 index27 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;request</code></div><div class="line number29 index28 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;responseCallback</code></div><div class="line number30 index29 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*/</code></div><div class="line number31 index30 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">public&nbsp;static&nbsp;void&nbsp;enqueue(Request&nbsp;request,&nbsp;Callback&nbsp;responseCallback){</code></div><div class="line number32 index31 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">mOkHttpClient.newCall(request).enqueue(responseCallback);</code></div><div class="line number33 index32 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number34 index33 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">/**</code></div><div class="line number35 index34 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;开启异步线程访问网络,&nbsp;且不在意返回结果(实现空callback)</code></div><div class="line number36 index35 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;request</code></div><div class="line number37 index36 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*/</code></div><div class="line number38 index37 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">public&nbsp;static&nbsp;void&nbsp;enqueue(Request&nbsp;request){</code></div><div class="line number39 index38 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">mOkHttpClient.newCall(request).enqueue(</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Callback()&nbsp;{</code></div><div class="line number40 index39 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number41 index40 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">@Override</code></div><div class="line number42 index41 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">public&nbsp;void&nbsp;onResponse(Response&nbsp;arg0)&nbsp;throws&nbsp;IOException&nbsp;{</code></div><div class="line number43 index42 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number44 index43 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number45 index44 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number46 index45 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">@Override</code></div><div class="line number47 index46 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">public&nbsp;void&nbsp;onFailure(Request&nbsp;arg0,&nbsp;IOException&nbsp;arg1)&nbsp;{</code></div><div class="line number48 index47 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number49 index48 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number50 index49 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">});</code></div><div class="line number51 index50 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number52 index51 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">public&nbsp;static&nbsp;String&nbsp;getStringFromServer(String&nbsp;url)&nbsp;throws&nbsp;IOException{</code></div><div class="line number53 index52 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder().url(url).build();</code></div><div class="line number54 index53 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;execute(request);</code></div><div class="line number55 index54 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(response.isSuccessful())&nbsp;{</code></div><div class="line number56 index55 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">String&nbsp;responseUrl&nbsp;=&nbsp;response.body().string();</code></div><div class="line number57 index56 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">responseUrl;</code></div><div class="line number58 index57 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}&nbsp;</code><code class="js keyword">else</code>&nbsp;<code class="js plain">{</code></div><div class="line number59 index58 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number60 index59 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number61 index60 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number62 index61 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">private&nbsp;static&nbsp;final&nbsp;String&nbsp;CHARSET_NAME&nbsp;=&nbsp;</code><code class="js string">"UTF-8"</code><code class="js plain">;</code></div><div class="line number63 index62 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">/**</code></div><div class="line number64 index63 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;这里使用了HttpClinet的API。只是为了方便</code></div><div class="line number65 index64 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;params</code></div><div class="line number66 index65 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@return</code></div><div class="line number67 index66 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*/</code></div><div class="line number68 index67 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">public&nbsp;static&nbsp;String&nbsp;formatParams(List&lt;BasicNameValuePair&gt;&nbsp;params){</code></div><div class="line number69 index68 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">URLEncodedUtils.format(params,&nbsp;CHARSET_NAME);</code></div><div class="line number70 index69 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number71 index70 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">/**</code></div><div class="line number72 index71 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;为HttpGet&nbsp;的&nbsp;url&nbsp;方便的添加多个name&nbsp;value&nbsp;参数。</code></div><div class="line number73 index72 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;url</code></div><div class="line number74 index73 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;params</code></div><div class="line number75 index74 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@return</code></div><div class="line number76 index75 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*/</code></div><div class="line number77 index76 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">public&nbsp;static&nbsp;String&nbsp;attachHttpGetParams(String&nbsp;url,&nbsp;List&lt;BasicNameValuePair&gt;&nbsp;params){</code></div><div class="line number78 index77 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">url&nbsp;+&nbsp;</code><code class="js string">"?"</code>&nbsp;<code class="js plain">+&nbsp;formatParams(params);</code></div><div class="line number79 index78 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number80 index79 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">/**</code></div><div class="line number81 index80 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;为HttpGet&nbsp;的&nbsp;url&nbsp;方便的添加1个name&nbsp;value&nbsp;参数。</code></div><div class="line number82 index81 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;url</code></div><div class="line number83 index82 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;name</code></div><div class="line number84 index83 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@param&nbsp;value</code></div><div class="line number85 index84 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*&nbsp;@return</code></div><div class="line number86 index85 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">*/</code></div><div class="line number87 index86 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">public&nbsp;static&nbsp;String&nbsp;attachHttpGetParam(String&nbsp;url,&nbsp;String&nbsp;name,&nbsp;String&nbsp;value){</code></div><div class="line number88 index87 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">url&nbsp;+&nbsp;</code><code class="js string">"?"</code>&nbsp;<code class="js plain">+&nbsp;name&nbsp;+&nbsp;</code><code class="js string">"="</code>&nbsp;<code class="js plain">+&nbsp;value;</code></div><div class="line number89 index88 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number7 index6 alt2">&nbsp;</div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number10 index9 alt1">&nbsp;</div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Headers&nbsp;responseHeaders&nbsp;=&nbsp;response.headers();</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">for</code>&nbsp;<code class="js plain">(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;responseHeaders.size();&nbsp;i++)&nbsp;{</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(responseHeaders.name(i)&nbsp;+&nbsp;</code><code class="js string">":&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;responseHeaders.value(i));</code></div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number15 index14 alt2">&nbsp;</div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number7 index6 alt2">&nbsp;</div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">client.newCall(request).enqueue(</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Callback()&nbsp;{</code></div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">@Override&nbsp;public&nbsp;void&nbsp;onFailure(Request&nbsp;request,&nbsp;Throwable&nbsp;throwable)&nbsp;{</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">throwable.printStackTrace();</code></div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number12 index11 alt1">&nbsp;</div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">@Override&nbsp;public&nbsp;void&nbsp;onResponse(Response&nbsp;response)&nbsp;throws&nbsp;IOException&nbsp;{</code></div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number15 index14 alt2">&nbsp;</div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Headers&nbsp;responseHeaders&nbsp;=&nbsp;response.headers();</code></div><div class="line number17 index16 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">for</code>&nbsp;<code class="js plain">(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;responseHeaders.size();&nbsp;i++)&nbsp;{</code></div><div class="line number18 index17 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(responseHeaders.name(i)&nbsp;+&nbsp;</code><code class="js string">":&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;responseHeaders.value(i));</code></div><div class="line number19 index18 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number20 index19 alt1">&nbsp;</div><div class="line number21 index20 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(response.body().string());</code></div><div class="line number22 index21 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number23 index22 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&lt;String, String&gt;</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&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.header(</code><code class="js string">"User-Agent"</code><code class="js plain">,&nbsp;</code><code class="js string">"OkHttp&nbsp;Headers.java"</code><code class="js plain">)</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.addHeader(</code><code class="js string">"Accept"</code><code class="js plain">,&nbsp;</code><code class="js string">"application/json;&nbsp;q=0.5"</code><code class="js plain">)</code></div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.addHeader(</code><code class="js string">"Accept"</code><code class="js plain">,&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number10 index9 alt1">&nbsp;</div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number13 index12 alt2">&nbsp;</div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Server:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;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">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Date:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;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">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Vary:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;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&nbsp;static&nbsp;final&nbsp;MediaType&nbsp;MEDIA_TYPE_MARKDOWN</code></div><div class="line number2 index1 alt1"><code class="js spaces">&nbsp;&nbsp;</code><code class="js plain">=&nbsp;MediaType.parse(</code><code class="js string">"text/x-markdown;&nbsp;charset=utf-8"</code><code class="js plain">);</code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="js plain">private&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2">&nbsp;</div><div class="line number6 index5 alt1"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">String&nbsp;postBody&nbsp;=&nbsp;</code><code class="js string">""</code></div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">+&nbsp;</code><code class="js string">"Releases\n"</code></div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">+&nbsp;</code><code class="js string">"--------\n"</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">+&nbsp;</code><code class="js string">"\n"</code></div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">+&nbsp;</code><code class="js string">"&nbsp;*&nbsp;_1.0_&nbsp;May&nbsp;6,&nbsp;2013\n"</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">+&nbsp;</code><code class="js string">"&nbsp;*&nbsp;_1.1_&nbsp;June&nbsp;15,&nbsp;2013\n"</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">+&nbsp;</code><code class="js string">"&nbsp;*&nbsp;_1.2_&nbsp;August&nbsp;11,&nbsp;2013\n"</code><code class="js plain">;</code></div><div class="line number14 index13 alt1">&nbsp;</div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.post(RequestBody.create(MEDIA_TYPE_MARKDOWN,&nbsp;postBody))</code></div><div class="line number18 index17 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number19 index18 alt2">&nbsp;</div><div class="line number20 index19 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number21 index20 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number22 index21 alt1">&nbsp;</div><div class="line number23 index22 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;static&nbsp;final&nbsp;MediaType&nbsp;MEDIA_TYPE_MARKDOWN</code></div><div class="line number2 index1 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">=&nbsp;MediaType.parse(</code><code class="js string">"text/x-markdown;&nbsp;charset=utf-8"</code><code class="js plain">);</code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="js plain">private&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2">&nbsp;</div><div class="line number6 index5 alt1"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">RequestBody&nbsp;requestBody&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">RequestBody()&nbsp;{</code></div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">@Override&nbsp;public&nbsp;MediaType&nbsp;contentType()&nbsp;{</code></div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">MEDIA_TYPE_MARKDOWN;</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number11 index10 alt2">&nbsp;</div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">@Override&nbsp;public&nbsp;void&nbsp;writeTo(BufferedSink&nbsp;sink)&nbsp;throws&nbsp;IOException&nbsp;{</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">for</code>&nbsp;<code class="js plain">(int&nbsp;i&nbsp;=&nbsp;2;&nbsp;i&nbsp;&lt;=&nbsp;997;&nbsp;i++)&nbsp;{</code></div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">sink.writeUtf8(String.format(</code><code class="js string">"&nbsp;*&nbsp;%s&nbsp;=&nbsp;%s\n"</code><code class="js plain">,&nbsp;i,&nbsp;factor(i)));</code></div><div class="line number17 index16 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number18 index17 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number19 index18 alt2">&nbsp;</div><div class="line number20 index19 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">private&nbsp;String&nbsp;factor(int&nbsp;n)&nbsp;{</code></div><div class="line number21 index20 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">for</code>&nbsp;<code class="js plain">(int&nbsp;i&nbsp;=&nbsp;2;&nbsp;i&nbsp;&lt;&nbsp;n;&nbsp;i++)&nbsp;{</code></div><div class="line number22 index21 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">int&nbsp;x&nbsp;=&nbsp;n&nbsp;/&nbsp;i;</code></div><div class="line number23 index22 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(x&nbsp;*&nbsp;i&nbsp;==&nbsp;n)&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">factor(x)&nbsp;+&nbsp;</code><code class="js string">"&nbsp;×&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;i;</code></div><div class="line number24 index23 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number25 index24 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">Integer.toString(n);</code></div><div class="line number26 index25 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number27 index26 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">};</code></div><div class="line number28 index27 alt1">&nbsp;</div><div class="line number29 index28 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number30 index29 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.post(requestBody)</code></div><div class="line number32 index31 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number33 index32 alt2">&nbsp;</div><div class="line number34 index33 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number35 index34 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number36 index35 alt1">&nbsp;</div><div class="line number37 index36 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;static&nbsp;final&nbsp;MediaType&nbsp;MEDIA_TYPE_MARKDOWN</code></div><div class="line number2 index1 alt1"><code class="js spaces">&nbsp;&nbsp;</code><code class="js plain">=&nbsp;MediaType.parse(</code><code class="js string">"text/x-markdown;&nbsp;charset=utf-8"</code><code class="js plain">);</code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="js plain">private&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2">&nbsp;</div><div class="line number6 index5 alt1"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">File&nbsp;file&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<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">&nbsp;</div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.post(RequestBody.create(MEDIA_TYPE_MARKDOWN,&nbsp;file))</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number13 index12 alt2">&nbsp;</div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number16 index15 alt1">&nbsp;</div><div class="line number17 index16 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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>&lt;form&gt;</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&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">RequestBody&nbsp;formBody&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">FormEncodingBuilder()</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.add(</code><code class="js string">"search"</code><code class="js plain">,&nbsp;</code><code class="js string">"Jurassic&nbsp;Park"</code><code class="js plain">)</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.post(formBody)</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number11 index10 alt2">&nbsp;</div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number14 index13 alt1">&nbsp;</div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;static&nbsp;final&nbsp;String&nbsp;IMGUR_CLIENT_ID&nbsp;=&nbsp;</code><code class="js string">"..."</code><code class="js plain">;</code></div><div class="line number2 index1 alt1"><code class="js plain">private&nbsp;static&nbsp;final&nbsp;MediaType&nbsp;MEDIA_TYPE_PNG&nbsp;=&nbsp;MediaType.parse(</code><code class="js string">"image/png"</code><code class="js plain">);</code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="js plain">private&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2">&nbsp;</div><div class="line number6 index5 alt1"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">//&nbsp;Use&nbsp;the&nbsp;imgur&nbsp;image&nbsp;upload&nbsp;API&nbsp;as&nbsp;documented&nbsp;at&nbsp;<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">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">RequestBody&nbsp;requestBody&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">MultipartBuilder()</code></div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.type(MultipartBuilder.FORM)</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.addPart(</code></div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Headers.of(</code><code class="js string">"Content-Disposition"</code><code class="js plain">,&nbsp;</code><code class="js string">"form-data;&nbsp;name=\"title\""</code><code class="js plain">),</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">RequestBody.create(</code><code class="js keyword">null</code><code class="js plain">,&nbsp;</code><code class="js string">"Square&nbsp;Logo"</code><code class="js plain">))</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.addPart(</code></div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Headers.of(</code><code class="js string">"Content-Disposition"</code><code class="js plain">,&nbsp;</code><code class="js string">"form-data;&nbsp;name=\"image\""</code><code class="js plain">),</code></div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">RequestBody.create(MEDIA_TYPE_PNG,&nbsp;</code><code class="js keyword">new</code>&nbsp;<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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number17 index16 alt2">&nbsp;</div><div class="line number18 index17 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number19 index18 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.header(</code><code class="js string">"Authorization"</code><code class="js plain">,&nbsp;</code><code class="js string">"Client-ID&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;IMGUR_CLIENT_ID)</code></div><div class="line number20 index19 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.post(requestBody)</code></div><div class="line number22 index21 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number23 index22 alt2">&nbsp;</div><div class="line number24 index23 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number25 index24 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number26 index25 alt1">&nbsp;</div><div class="line number27 index26 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1"><code class="js plain">private&nbsp;final&nbsp;Gson&nbsp;gson&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Gson();</code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number10 index9 alt1">&nbsp;</div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Gist&nbsp;gist&nbsp;=&nbsp;gson.fromJson(response.body().charStream(),&nbsp;Gist.class);</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">for</code>&nbsp;<code class="js plain">(Map.Entry&lt;String,&nbsp;GistFile&gt;&nbsp;entry&nbsp;:&nbsp;gist.files.entrySet())&nbsp;{</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(entry.getKey());</code></div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(entry.getValue().content);</code></div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;</div><div class="line number18 index17 alt1"><code class="js plain">static&nbsp;class&nbsp;Gist&nbsp;{</code></div><div class="line number19 index18 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Map&lt;String,&nbsp;GistFile&gt;&nbsp;files;</code></div><div class="line number20 index19 alt1"><code class="js plain">}</code></div><div class="line number21 index20 alt2">&nbsp;</div><div class="line number22 index21 alt1"><code class="js plain">static&nbsp;class&nbsp;GistFile&nbsp;{</code></div><div class="line number23 index22 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">String&nbsp;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&nbsp;final&nbsp;OkHttpClient&nbsp;client;</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="js plain">public&nbsp;CacheResponse(File&nbsp;cacheDirectory)&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">int&nbsp;cacheSize&nbsp;=&nbsp;10&nbsp;*&nbsp;1024&nbsp;*&nbsp;1024;&nbsp;</code><code class="js comments">//&nbsp;10&nbsp;MiB</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Cache&nbsp;cache&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Cache(cacheDirectory,&nbsp;cacheSize);</code></div><div class="line number6 index5 alt1">&nbsp;</div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;</div><div class="line number11 index10 alt2"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number15 index14 alt2">&nbsp;</div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response1&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number17 index16 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response1.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response1);</code></div><div class="line number18 index17 alt1">&nbsp;</div><div class="line number19 index18 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">String&nbsp;response1Body&nbsp;=&nbsp;response1.body().string();</code></div><div class="line number20 index19 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;1&nbsp;response:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response1);</code></div><div class="line number21 index20 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;1&nbsp;cache&nbsp;response:&nbsp;&nbsp;&nbsp;&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response1.cacheResponse());</code></div><div class="line number22 index21 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;1&nbsp;network&nbsp;response:&nbsp;&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response1.networkResponse());</code></div><div class="line number23 index22 alt2">&nbsp;</div><div class="line number24 index23 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response2&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number25 index24 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response2.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response2);</code></div><div class="line number26 index25 alt1">&nbsp;</div><div class="line number27 index26 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">String&nbsp;response2Body&nbsp;=&nbsp;response2.body().string();</code></div><div class="line number28 index27 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;2&nbsp;response:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response2);</code></div><div class="line number29 index28 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;2&nbsp;cache&nbsp;response:&nbsp;&nbsp;&nbsp;&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response2.cacheResponse());</code></div><div class="line number30 index29 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;2&nbsp;network&nbsp;response:&nbsp;&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response2.networkResponse());</code></div><div class="line number31 index30 alt2">&nbsp;</div><div class="line number32 index31 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;2&nbsp;equals&nbsp;Response&nbsp;1?&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;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",&nbsp;"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",&nbsp;"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>&nbsp;<code class="js plain">{</code></div><div class="line number2 index1 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">connection.addRequestProperty(</code><code class="js string">"Cache-Control"</code><code class="js plain">,&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">InputStream&nbsp;cached&nbsp;=&nbsp;connection.getInputStream();</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">//&nbsp;the&nbsp;resource&nbsp;was&nbsp;cached!&nbsp;show&nbsp;it</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;</code><code class="js keyword">catch</code>&nbsp;<code class="js plain">(FileNotFoundException&nbsp;e)&nbsp;{</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">//&nbsp;the&nbsp;resource&nbsp;was&nbsp;not&nbsp;cached</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;</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&nbsp;maxStale&nbsp;=&nbsp;60&nbsp;*&nbsp;60&nbsp;*&nbsp;24&nbsp;*&nbsp;28;&nbsp;</code><code class="js comments">//&nbsp;tolerate&nbsp;4-weeks&nbsp;staleconnection.addRequestProperty("Cache-Control",&nbsp;"max-stale="&nbsp;+&nbsp;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&nbsp;final&nbsp;ScheduledExecutorService&nbsp;executor&nbsp;=&nbsp;Executors.newScheduledThreadPool(1);</code></div><div class="line number2 index1 alt1"><code class="js plain">private&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">)&nbsp;//&nbsp;This&nbsp;URL&nbsp;is&nbsp;served&nbsp;</code><code class="js keyword">with</code>&nbsp;<code class="js plain">a&nbsp;2&nbsp;second&nbsp;delay.</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number8 index7 alt1">&nbsp;</div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">final&nbsp;long&nbsp;startNanos&nbsp;=&nbsp;System.nanoTime();</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">final&nbsp;Call&nbsp;call&nbsp;=&nbsp;client.newCall(request);</code></div><div class="line number11 index10 alt2">&nbsp;</div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js comments">//&nbsp;Schedule&nbsp;a&nbsp;job&nbsp;to&nbsp;cancel&nbsp;the&nbsp;call&nbsp;in&nbsp;1&nbsp;second.</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">executor.schedule(</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Runnable()&nbsp;{</code></div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">@Override&nbsp;public&nbsp;void&nbsp;run()&nbsp;{</code></div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f&nbsp;Canceling&nbsp;call.%n"</code><code class="js plain">,&nbsp;(System.nanoTime()&nbsp;-&nbsp;startNanos)&nbsp;/&nbsp;1e9f);</code></div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">call.cancel();</code></div><div class="line number17 index16 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f&nbsp;Canceled&nbsp;call.%n"</code><code class="js plain">,&nbsp;(System.nanoTime()&nbsp;-&nbsp;startNanos)&nbsp;/&nbsp;1e9f);</code></div><div class="line number18 index17 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number19 index18 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">},&nbsp;1,&nbsp;TimeUnit.SECONDS);</code></div><div class="line number20 index19 alt1">&nbsp;</div><div class="line number21 index20 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">try</code>&nbsp;<code class="js plain">{</code></div><div class="line number22 index21 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f&nbsp;Executing&nbsp;call.%n"</code><code class="js plain">,&nbsp;(System.nanoTime()&nbsp;-&nbsp;startNanos)&nbsp;/&nbsp;1e9f);</code></div><div class="line number23 index22 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;call.execute();</code></div><div class="line number24 index23 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f&nbsp;Call&nbsp;was&nbsp;expected&nbsp;to&nbsp;fail,&nbsp;but&nbsp;completed:&nbsp;%s%n"</code><code class="js plain">,</code></div><div class="line number25 index24 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">(System.nanoTime()&nbsp;-&nbsp;startNanos)&nbsp;/&nbsp;1e9f,&nbsp;response);</code></div><div class="line number26 index25 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}&nbsp;</code><code class="js keyword">catch</code>&nbsp;<code class="js plain">(IOException&nbsp;e)&nbsp;{</code></div><div class="line number27 index26 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.printf(</code><code class="js string">"%.2f&nbsp;Call&nbsp;failed&nbsp;as&nbsp;expected:&nbsp;%s%n"</code><code class="js plain">,</code></div><div class="line number28 index27 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">(System.nanoTime()&nbsp;-&nbsp;startNanos)&nbsp;/&nbsp;1e9f,&nbsp;e);</code></div><div class="line number29 index28 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;final&nbsp;OkHttpClient&nbsp;client;</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="js plain">public&nbsp;ConfigureTimeouts()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">client.setConnectTimeout(10,&nbsp;TimeUnit.SECONDS);</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">client.setWriteTimeout(10,&nbsp;TimeUnit.SECONDS);</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">client.setReadTimeout(30,&nbsp;TimeUnit.SECONDS);</code></div><div class="line number8 index7 alt1"><code class="js plain">}</code></div><div class="line number9 index8 alt2">&nbsp;</div><div class="line number10 index9 alt1"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">)&nbsp;//&nbsp;This&nbsp;URL&nbsp;is&nbsp;served&nbsp;</code><code class="js keyword">with</code>&nbsp;<code class="js plain">a&nbsp;2&nbsp;second&nbsp;delay.</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number14 index13 alt1">&nbsp;</div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;completed:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;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&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">)&nbsp;//&nbsp;This&nbsp;URL&nbsp;is&nbsp;served&nbsp;</code><code class="js keyword">with</code>&nbsp;<code class="js plain">a&nbsp;1&nbsp;second&nbsp;delay.</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number7 index6 alt2">&nbsp;</div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">try</code>&nbsp;<code class="js plain">{</code></div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.clone()&nbsp;</code><code class="js comments">//&nbsp;Clone&nbsp;to&nbsp;make&nbsp;a&nbsp;customized&nbsp;OkHttp&nbsp;for&nbsp;this&nbsp;request.</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.setReadTimeout(500,&nbsp;TimeUnit.MILLISECONDS)</code></div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.newCall(request)</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.execute();</code></div><div class="line number13 index12 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;1&nbsp;succeeded:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}&nbsp;</code><code class="js keyword">catch</code>&nbsp;<code class="js plain">(IOException&nbsp;e)&nbsp;{</code></div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;1&nbsp;failed:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;e);</code></div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number17 index16 alt2">&nbsp;</div><div class="line number18 index17 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">try</code>&nbsp;<code class="js plain">{</code></div><div class="line number19 index18 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.clone()&nbsp;</code><code class="js comments">//&nbsp;Clone&nbsp;to&nbsp;make&nbsp;a&nbsp;customized&nbsp;OkHttp&nbsp;for&nbsp;this&nbsp;request.</code></div><div class="line number20 index19 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.setReadTimeout(3000,&nbsp;TimeUnit.MILLISECONDS)</code></div><div class="line number21 index20 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.newCall(request)</code></div><div class="line number22 index21 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.execute();</code></div><div class="line number23 index22 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;2&nbsp;succeeded:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number24 index23 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}&nbsp;</code><code class="js keyword">catch</code>&nbsp;<code class="js plain">(IOException&nbsp;e)&nbsp;{</code></div><div class="line number25 index24 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Response&nbsp;2&nbsp;failed:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;e);</code></div><div class="line number26 index25 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;List&lt;Challenge&gt;&nbsp;challenges()</code></div><div class="line number2 index1 alt1"><code class="js plain">Returns&nbsp;the&nbsp;authorization&nbsp;challenges&nbsp;appropriate&nbsp;</code><code class="js keyword">for</code>&nbsp;<code class="js keyword">this</code>&nbsp;<code class="js plain">response's&nbsp;code.&nbsp;</code></div><div class="line number3 index2 alt2"><code class="js plain">If&nbsp;the&nbsp;response&nbsp;code&nbsp;is&nbsp;401&nbsp;unauthorized,&nbsp;</code></div><div class="line number4 index3 alt1"><code class="js keyword">this</code>&nbsp;<code class="js plain">returns&nbsp;the&nbsp;</code><code class="js string">"WWW-Authenticate"</code>&nbsp;<code class="js plain">challenges.</code></div><div class="line number5 index4 alt2"><code class="js plain">If&nbsp;the&nbsp;response&nbsp;code&nbsp;is&nbsp;407&nbsp;proxy&nbsp;unauthorized,&nbsp;</code><code class="js keyword">this</code>&nbsp;<code class="js plain">returns&nbsp;the&nbsp;</code><code class="js string">"Proxy-Authenticate"</code>&nbsp;<code class="js plain">challenges.</code></div><div class="line number6 index5 alt1"><code class="js plain">Otherwise&nbsp;</code><code class="js keyword">this</code>&nbsp;<code class="js plain">returns&nbsp;an&nbsp;empty&nbsp;list&nbsp;of&nbsp;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&nbsp;final&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">OkHttpClient();</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="js plain">public&nbsp;void&nbsp;run()&nbsp;throws&nbsp;Exception&nbsp;{</code></div><div class="line number4 index3 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">client.setAuthenticator(</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Authenticator()&nbsp;{</code></div><div class="line number5 index4 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">@Override&nbsp;public&nbsp;Request&nbsp;authenticate(Proxy&nbsp;proxy,&nbsp;Response&nbsp;response)&nbsp;{</code></div><div class="line number6 index5 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Authenticating&nbsp;for&nbsp;response:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number7 index6 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">System.out.println(</code><code class="js string">"Challenges:&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response.challenges());</code></div><div class="line number8 index7 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">String&nbsp;credential&nbsp;=&nbsp;Credentials.basic(</code><code class="js string">"jesse"</code><code class="js plain">,&nbsp;</code><code class="js string">"password1"</code><code class="js plain">);</code></div><div class="line number9 index8 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js plain">response.request().newBuilder()</code></div><div class="line number10 index9 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.header(</code><code class="js string">"Authorization"</code><code class="js plain">,&nbsp;credential)</code></div><div class="line number11 index10 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number12 index11 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number13 index12 alt2">&nbsp;</div><div class="line number14 index13 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">@Override&nbsp;public&nbsp;Request&nbsp;authenticateProxy(Proxy&nbsp;proxy,&nbsp;Response&nbsp;response)&nbsp;{</code></div><div class="line number15 index14 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">return</code>&nbsp;<code class="js keyword">null</code><code class="js plain">;&nbsp;</code><code class="js comments">//&nbsp;Null&nbsp;indicates&nbsp;no&nbsp;attempt&nbsp;to&nbsp;authenticate.</code></div><div class="line number16 index15 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">}</code></div><div class="line number17 index16 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">});</code></div><div class="line number18 index17 alt1">&nbsp;</div><div class="line number19 index18 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Request&nbsp;request&nbsp;=&nbsp;</code><code class="js keyword">new</code>&nbsp;<code class="js plain">Request.Builder()</code></div><div class="line number20 index19 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">.build();</code></div><div class="line number22 index21 alt1">&nbsp;</div><div class="line number23 index22 alt2"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js plain">Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();</code></div><div class="line number24 index23 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="js keyword">if</code>&nbsp;<code class="js plain">(!response.isSuccessful())&nbsp;</code><code class="js keyword">throw</code>&nbsp;<code class="js keyword">new</code>&nbsp;<code class="js plain">IOException(</code><code class="js string">"Unexpected&nbsp;code&nbsp;"</code>&nbsp;<code class="js plain">+&nbsp;response);</code></div><div class="line number25 index24 alt2">&nbsp;</div><div class="line number26 index25 alt1"><code class="js spaces">&nbsp;&nbsp;&nbsp;&nbsp;</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>