HttpUrlConnection

最开始学android的时候用的网络请求是HttpUrlConnection,当时很多东西还不知道,但是在android 2.2及以下版本中HttpUrlConnection存在着一些bug,所以建议在android 2.3以后使用HttpUrlConnection,之前使用HttpClient。

在Android 2.2版本之前,HttpClient拥有较少的bug,因此使用它是最好的选择。而在Android 2.3版本及以后,HttpURLConnection则是最佳的选择。它的API简单,体积较小,因而非常适用于Android项目。压缩和缓存机制可以有效地减少网络访问的流量,在提升速度和省电方面也起到了较大的作用。对于新的应用程序应该更加偏向于使用HttpURLConnection,因为在以后的工作当中我们也会将更多的时间放在优化HttpURLConnection上面。

特点

  • 比较轻便,灵活,易于扩展
  • 在3.0后以及4.0中都进行了改善,如对HTTPS的支持
  • 在4.0中,还增加了对缓存的支持

用法

  1. 首先我们需要获取到一个HttpURLConnection实例,一般需要new出一个URL对象,并传入目标网络地址,通过调用openConnection()方法获得HttpURLConnection实例。
  2. 得到该实例后。我们需要设置一下http请求的的方法,这里我们主要研究get和post,默认是使用get方法。get一般用于从服务器获取数据,post一般用于向服务器提交数据,设置请求方法使用函数setRequestMethod(“POST”)进行设置。
  3. 此外可以进行一些请求的限制,比如连接超时的时间等,可以通过setConnectTimeout设置超时时间。
  4. 获取服务器返回的输入流,使用getInputStream方法获取。
  5. 读取内容并处理
  6. 关闭连接,通过调用disconnect方法关闭当前的连接。 
    关键代码如下 
    使用过程中不要忘记添加权限
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">  <uses-permission android:<span class="hljs-property" style="box-sizing: border-box;">name=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"android.permission.INTERNET"</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
  • GET
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public String <span class="hljs-title" style="box-sizing: border-box;">get(String urlPath) { 
        HttpURLConnection connection = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
        InputStream <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try { 
            URL url = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new URL(urlPath); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得URL对象 
            connection = (HttpURLConnection) url.openConnection(); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得HttpURLConnection对象 
            connection.setRequestMethod(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"GET"); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 默认为GET 
            connection.setUseCaches(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//不使用缓存 
            connection.setConnectTimeout(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10000); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置超时时间 
            connection.setReadTimeout(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10000); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置读取超时时间 
            connection.setDoInput(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置是否从httpUrlConnection读入,默认情况下是true; 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相应码是否为200 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is = connection.getInputStream(); 
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得输入流 
                BufferedReader reader = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new BufferedReader(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new InputStreamReader(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is)); 
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//包装字节流为字符流 
                StringBuilder response = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new StringBuilder(); 
                String line; 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while ((line = reader.readLine()) != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null) { 
                    response.append(line); 
                } 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return response.toString(); 
            } 
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch (Exception e) { 
            e.printStackTrace(); 
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">finally { 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if (connection != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null) { 
                connection.disconnect(); 
                connection = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
            } 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null) { 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try { 
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is.close(); 
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
                } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } 
        } 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li></ul>
  • POST
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private String <span class="hljs-title" style="box-sizing: border-box;">post(String urlPath, Map<String, String> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params) { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null || <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params.size() == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0) { 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get(urlPath); 
        } 
        OutputStream os = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
        InputStream <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
        HttpURLConnection connection = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
        StringBuffer body = getParamString(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params); 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte[] data = body.toString().getBytes(); 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try { 
            URL url = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new URL(urlPath); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得URL对象 
            connection = (HttpURLConnection) url.openConnection(); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得HttpURLConnection对象 
            connection.setRequestMethod(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"POST"); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置请求方法为post 
            connection.setUseCaches(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//不使用缓存 
            connection.setConnectTimeout(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10000); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置超时时间 
            connection.setReadTimeout(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10000); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置读取超时时间 
            connection.setDoInput(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置是否从httpUrlConnection读入,默认情况下是true; 
            connection.setDoOutput(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置为true后才能写入参数 
            connection.setRequestProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Content-Type", <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"application/x-www-form-urlencoded"); 
            connection.setRequestProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Content-Length", String.valueOf(data.length)); 
            os = connection.getOutputStream(); 
            os.write(data); 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//写入参数 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相应码是否为200 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is = connection.getInputStream(); 
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得输入流 
                BufferedReader reader = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new BufferedReader(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new InputStreamReader(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is)); 
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//包装字节流为字符流 
                StringBuilder response = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new StringBuilder(); 
                String line; 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while ((line = reader.readLine()) != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null) { 
                    response.append(line); 
                } 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return response.toString(); 
            } 
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch (Exception e) { 
            e.printStackTrace(); 
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">finally { 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//关闭 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if (os != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null) { 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try { 
                    os.close(); 
                } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null) { 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try { 
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is.close(); 
                } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if (connection != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null) { 
                connection.disconnect(); 
                connection = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
            } 
        } 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
    } 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private StringBuffer <span class="hljs-title" style="box-sizing: border-box;">getParamString(Map<String, String> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params) { 
        StringBuffer result = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new StringBuffer(); 
        Iterator<Map.Entry<String, String>> iterator = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params.entrySet().iterator(); 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while (iterator.hasNext()) { 
            Map.Entry<String, String> param = iterator.next(); 
            String key = param.getKey(); 
            String <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">value = param.getValue(); 
            result.append(key).append(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'=').append(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">value); 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if (iterator.hasNext()) { 
                result.append(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'&'); 
            } 
        } 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return result; 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li></ul>

以上代码参考了部分LessCode项目

HttpClient

特点

  • 高效稳定,但是维护成本高昂,故android 开发团队不愿意在维护该库而是转投更为轻便的HttpUrlConnection

用法

  1. HttpClient是一个接口,因此无法直接创建它的实例,一般都是创建一个DefaultHttpClient实例
  2. 如果要发起Get请求,需要创建一个HttpGet对象,并传入请求地址
  3. 如果要发起Post请求,需要创建一个HttpPost对象。并传入请求地址,通过setEntity函数设置请求参数
  4. 调用execute方法,传入HttpGet或者HttpPost实例,执行后返回HttpResponse对象,判断响应状态码
  5. 解析响应结果,通过调用getEntity函数获得一个HttpEntity对象,之后可以通过EntityUtils.toString方法将其转换为字符串

由于在android2.3之后就被HttpUrlConnection取代了,这里也不过多介绍了,不过当初学习它的时候还没接触到其他库,就感觉它好方便,下面简单贴出使用方法

  • GET
<code class="hljs vbscript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String url){ 
        HttpClient client=<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
        HttpGet <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request=<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
        try { 
            client=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new DefaultHttpClient(); 
            <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new HttpGet(url); 
            HttpResponse <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response=client.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">execute(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request); 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){ 
                <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String result=EntityUtils.toString(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response.getEntity(),<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"UTF-8"); 
                return result; 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return  <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>
  • POST
<code class="hljs vbscript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String post(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String url,List<NameValuePair> params){ 
        HttpClient client=<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
        HttpPost <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request=<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
        try { 
            client=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new DefaultHttpClient(); 
            <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new HttpPost(url); 
            <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request.setEntity(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
            HttpResponse <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response=client.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">execute(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request); 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){ 
                <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String result=EntityUtils.toString(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response.getEntity(),<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"UTF-8"); 
                return result; 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return  <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>

以上代码参考了郭霖《第一行代码》——HttpClient部分

Android Asynchronous Http Client

Android Asynchronous Http Client一看名字就知道它是基于Http Client的,但是呢在安卓中Http Client已经废弃了,所以也不建议使用这个库了。然后仍然有一些可取的内容值得学习,所以这里也介绍一下。

特点

  • 所以请求在子线程中完成,请求回调在调用该请求的线程中完成
  • 使用线程池
  • 使用RequestParams类封装请求参数
  • 支持文件上传
  • 持久化cookie到SharedPreferences,个人感觉这一点也是这个库的重要特点,可以很方便的完成一些模拟登录
  • 支持json
  • 支持HTTP Basic Auth

用法

  • 编写一个静态的HttpClient
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package cn.edu.zafu.http; 
 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import com.loopj.android.http.AsyncHttpClient; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import com.loopj.android.http.AsyncHttpResponseHandler; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import com.loopj.android.http.RequestParams; 
 
<span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** 
 * Created by lizhangqu on 2015/5/7. 
 */ 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">TestClient { 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final String BASE_URL = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://121.41.119.107/"; 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static AsyncHttpClient client = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new AsyncHttpClient(); 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { 
        client.get(getAbsoluteUrl(url), params, responseHandler); 
    } 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { 
        client.post(getAbsoluteUrl(url), params, responseHandler); 
    } 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static String <span class="hljs-title" style="box-sizing: border-box;">getAbsoluteUrl(String relativeUrl) { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return BASE_URL + relativeUrl; 
    } 
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul>
  • 调用get或者post方法

参数通过RequestParams传递,没有参数则传递null

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">RequestParams  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new RequestParams(); 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"",<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"");</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
  • 如果要保存cookie,在发起请求之前调用以下代码
<code class="hljs axapta has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">PersistentCookieStore myCookieStore = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new PersistentCookieStore(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this); 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">client.setCookieStore(myCookieStore);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

之后请求所得到的cookie都会自动持久化

如果要自己添加cookie,则调用以下代码

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">BasicClientCookie newCookie = new BasicClientCookie(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cookiesare", <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"awesome")<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
newCookie<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setVersion(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
newCookie<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDomain(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mydomain.com")<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
newCookie<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setPath(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/")<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
myCookieStore<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.addCookie(newCookie)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
  • 使用 
    在回调函数中处理返回结果
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">get(){ 
        TestClient.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test/index.php", <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new AsyncHttpResponseHandler() { 
            @Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onSuccess(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int statusCode, Header[] headers, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte[] responseBody) { 
 
            } 
 
            @Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onFailure(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int statusCode, Header[] headers, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte[] responseBody, Throwable error) { 
 
            } 
        }); 
    } 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">post(){ 
        RequestParams <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new RequestParams(); 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user",<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"asas"); 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"pass",<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"12121"); 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"time",<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1212121"); 
        TestClient.post(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test/login.php", <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new AsyncHttpResponseHandler() { 
            @Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onSuccess(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int statusCode, Header[] headers, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte[] responseBody) { 
 
            } 
 
            @Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onFailure(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int statusCode, Header[] headers, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte[] responseBody, Throwable error) { 
 
            } 
        }); 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li></ul>

以上代码参考了Android Asynchronous Http Client官方实例

Volley

既然在android2.2之后不建议使用Http Client,那么有没有一个库是android2.2及以下版本使用Http Client,而android2.3及以上版本使用HttpUrlConnection的呢,答案是肯定的,就是Volley,它是android开发团队在2013年Google I/O大会上推出了一个新的网络通信框架

Volley可以说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可以像AsyncHttpClient一样非常简单地进行HTTP通信,也可以像Universal-Image-Loader一样轻松加载网络上的图片。除了简单易用之外,Volley在性能方面也进行了大幅度的调整,它的设计目标就是非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕

特点

  1. Volley的优势在于处理小文件的http请求;
  2. 在Volley中也是可以使用Okhttp作为传输层
  3. Volley在处理高分辨率的图像压缩上有很好的支持;
  4. NetworkImageView在GC的使用模式上更加保守,在请求清理上也更加积极,networkimageview仅仅依赖于强大的内存引用,并当一个新请求是来自ImageView或ImageView离开屏幕时 会清理掉所有的请求数据。

用法

  1. 创建一个RequestQueue对象。
  2. 创建一个Request对象。
  3. 将Request对象添加到RequestQueue里面。

下面一步一步来学习其用法

  • GET
<code class="hljs vbscript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private void <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get(){ 
        RequestQueue queue= Volley.newRequestQueue(getApplicationContext()); 
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String url=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://121.41.119.107/test/index.php"; 
        StringRequest <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new StringRequest(url, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Response.Listener<<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String>() { 
            @Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public void onResponse(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response) { 
                <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Log.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TAG",<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response); 
            } 
        }, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Response.ErrorListener() { 
            @Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public void onErrorResponse(VolleyError <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">error) { 
 
            } 
        }); 
        queue.add(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request); 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>
  • POST 
    通过指定请求方法为Request.Method.POST使其成为post请求,然后重新getParams方法设置请求参数。当发出POST请求的时候,Volley会尝试调用StringRequest的父类——Request中的getParams()方法来获取POST参数
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void post() { 
        RequestQueue <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">queue <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">= Volley<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.newRequestQueue(getApplicationContext()); 
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String url <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">= <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://121.41.119.107/test/login.php"; 
        StringRequest request <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">= <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new StringRequest(Request<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.Method<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.POST, url, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new Response<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.Listener<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">>() { 
            @Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void onResponse(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String response) { 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Log<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TAG", response); 
            } 
        }, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new Response<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.ErrorListener() { 
            @Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void onErrorResponse(VolleyError error) { 
 
            } 
        }) { 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//重写getParams方法设置参数 
            @Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Map<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">> getParams() throws AuthFailureError { 
                <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Map<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">= <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new HashMap<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">>(); 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user", <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"asas"); 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"pass", <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"12121"); 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"time", <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1212121"); 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params; 
            } 
        }; 
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">queue<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.add(request); 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul>
  • 加载图片 
    加载图像的方法和前面类似,只不过不在是StringRequest而是ImageRequest。
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">getImage() { 
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); 
        String url = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"https://www.baidu.com/img/bdlogo.png"; 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//第三第四个参数分别用于指定允许图片最大的宽度和高度,如果指定的网络图片的宽度或高度大于这里的最大值,则会对图片进行压缩,指定成0的话就表示不管图片有多大,都不会进行压缩。 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//第五个参数就是ImageView里中的属性ScaleType 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//第六个参数用于指定图片的颜色属性 
        ImageRequest request = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new ImageRequest(url, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new Response.Listener<Bitmap>() { 
            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onResponse(Bitmap response) { 
                ImageView iv= (ImageView) findViewById(R.id.iv); 
                iv.setImageBitmap(response); 
            } 
        }, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0, ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_8888, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new Response.ErrorListener() { 
 
            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onErrorResponse(VolleyError error) { 
 
            } 
        }); 
        queue.add(request); 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul>

其实加载图片的功能还远远不止这些,使用ImageLoader可以实现对图片的缓存,还可以过滤重复链接,避免发送重复的请求 
ImageLoader的使用方法概括为以下几步 
1. 创建一个RequestQueue对象。 
2. 创建一个ImageLoader对象。 
3. 获取一个ImageListener对象。 
4. 调用ImageLoader的get()方法加载网络上的图片。

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//继承ImageCache,使用LruCache实现缓存 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">BitmapCache <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">implements <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ImageLoader.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ImageCache { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private LruCache<String, Bitmap> mCache; 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-title" style="box-sizing: border-box;">BitmapCache() { 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int maxSize = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10 * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1024 * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1024; 
            mCache = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new LruCache<String, Bitmap>(maxSize) { 
                <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int <span class="hljs-title" style="box-sizing: border-box;">sizeOf(String key, Bitmap bitmap) { 
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return bitmap.getRowBytes() * bitmap.getHeight(); 
                } 
            }; 
        } 
        <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public Bitmap <span class="hljs-title" style="box-sizing: border-box;">getBitmap(String url) { 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return mCache.get(url); 
        } 
        <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">putBitmap(String url, Bitmap bitmap) { 
            mCache.put(url, bitmap); 
        } 
 
    } 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">getImageByImageLoader() { 
        ImageView iv= (ImageView) findViewById(R.id.iv); 
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); 
        String url = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"https://www.baidu.com/img/bdlogo.png"; 
        ImageLoader loader=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new ImageLoader(queue,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new BitmapCache() ); 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第一个参数指定用于显示图片的ImageView控件 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第二个参数指定加载图片的过程中显示的图片 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第三个参数指定加载图片失败的情况下显示的图片 
        ImageLoader.ImageListener listener=ImageLoader.getImageListener(iv,R.mipmap.ic_launcher,R.mipmap.ic_launcher); 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 调用ImageLoader的get()方法来加载图片 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第一个参数就是图片的URL地址 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第二个参数则是刚刚获取到的ImageListener对象 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 如果想对图片的大小进行限制,也可以使用get()方法的重载,指定图片允许的最大宽度和高度,即通过第三第四个参数指定 
        loader.get(url,listener); 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li></ul>

最后,Volley提供了一种自定义ImageView来加载图片,其使用方法可概括为 
1. 创建一个RequestQueue对象。 
2. 创建一个ImageLoader对象。 
3. 在布局文件中添加一个NetworkImageView控件。 
4. 在代码中获取该控件的实例。 
5. 设置要加载的图片地址。

我们在布局中申明该控件

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.volley<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.toolbox<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.NetworkImageView 
        android:id=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"@+id/network_image_view" 
        android:layout_width=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"wrap_content" 
        android:layout_height=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"wrap_content" 
        android:layout_centerInParent=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"true" 
        /></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

在程序中实现加载

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">public void networkImageView(){ 
        RequestQueue queue = Volley<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.newRequestQueue(getApplicationContext())<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
        ImageLoader loader=new ImageLoader(queue,new BitmapCache() )<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
        NetworkImageView niv= (NetworkImageView) findViewById(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.network_image_view)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
        niv<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDefaultImageResId(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mipmap<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ic_launcher)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//设置加载中显示的图片 
        niv<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setErrorImageResId(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mipmap<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ic_launcher)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//设置加载失败时显示的图片 
        niv<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setImageUrl(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"https://www.baidu.com/img/bdlogo.png",  loader)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//设置目标图片的URL地址 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
  • 自定义Request

在实际应用中,往往需要将http请求与json进行集成,而Volley正恰恰支持这样的方式,不过需要我们自己自定义Request,这里我们使用google的Gson库进行集成。 
1. 继承Request类 
2. 重写parseNetworkResponse,实现json与实体类转换,由于实体类未定,所以采用泛型

下文用到的json字符串如下

<code class="hljs json has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">{"<span class="hljs-attribute" style="box-sizing: border-box;">name":<span class="hljs-value" style="box-sizing: border-box;"><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lizhangqu","<span class="hljs-attribute" style="box-sizing: border-box;">age":<span class="hljs-value" style="box-sizing: border-box;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
<code class="hljs scala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package cn.edu.zafu.http; 
 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import com.android.volley.NetworkResponse; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import com.android.volley.ParseError; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import com.android.volley.Request; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import com.android.volley.Response; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import com.android.volley.toolbox.HttpHeaderParser; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import com.google.gson.Gson; 
 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import java.io.UnsupportedEncodingException; 
 
<span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** 
 * Created by lizhangqu on 2015/5/7. 
 */ 
public <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">GsonRequest<<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">T> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Request<<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">T> { 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final Response.Listener<T> mListener; 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private Gson mGson; 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private Class<T> mClass; 
 
    public GsonRequest(int method, String url, Class<T> clazz, Response.Listener<T> listener, 
                       Response.ErrorListener errorListener) { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super(method, url, errorListener); 
        mGson = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new Gson(); 
        mClass = clazz; 
        mListener = listener; 
    } 
 
    public GsonRequest(String url, Class<T> clazz, Response.Listener<T> listener, 
                       Response.ErrorListener errorListener) { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this(Method.GET, url, clazz, listener, errorListener); 
    } 
 
    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected Response<T> parseNetworkResponse(NetworkResponse response) { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try { 
            String jsonString = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new String(response.data, 
                    HttpHeaderParser.parseCharset(response.headers)); 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return Response.success(mGson.fromJson(jsonString, mClass), 
                    HttpHeaderParser.parseCacheHeaders(response)); 
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch (UnsupportedEncodingException e) { 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return Response.error(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new ParseError(e)); 
        } 
    } 
 
    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected void deliverResponse(T response) { 
        mListener.onResponse(response); 
    } 
 
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li></ul>

编写测试实体类,两个字段一个name一个age

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package cn.edu.zafu.http; 
 
<span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** 
 * Created by lizhangqu on 2015/5/7. 
 */ 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Person { 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private String name; 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int age; 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public String <span class="hljs-title" style="box-sizing: border-box;">getName() { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return name; 
    } 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">setName(String name) { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this.name = name; 
    } 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int <span class="hljs-title" style="box-sizing: border-box;">getAge() { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return age; 
    } 
 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">setAge(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int age) { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this.age = age; 
    } 
 
    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public String <span class="hljs-title" style="box-sizing: border-box;">toString() { 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Person{" + 
                <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name='" + name + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'\'' + 
                <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", age=" + age + 
                <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'}'; 
    } 
} 
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li></ul>

调用方法和StringRequest是一样的。如下所示

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">json(){ 
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); 
        String url = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://121.41.119.107/test/index.php"; 
        GsonRequest<Person> request=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new GsonRequest<Person>(url, Person.class, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new Response.Listener<Person>() { 
            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onResponse(Person response) { 
                Log.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TAG",response.toString()); 
            } 
        }, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new Response.ErrorListener() { 
            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onErrorResponse(VolleyError error) { 
 
            } 
        }); 
        queue.add(request); 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>

以上代码参考了郭霖三篇Volley博客文章,分别为 
Android Volley完全解析(一),初识Volley的基本用法 
Android Volley完全解析(二),使用Volley加载网络图片 
Android Volley完全解析(三),定制自己的Request

okHttp

okhttp 是一个 Java 的 HTTP+SPDY 客户端开发包,同时也支持 Android。需要Android 2.3以上。

特点

  • OKHttp是Android版Http客户端。非常高效,支持SPDY、连接池、GZIP和 HTTP 缓存。
  • 默认情况下,OKHttp会自动处理常见的网络问题,像二次连接、SSL的握手问题。
  • 如果你的应用程序中集成了OKHttp,Retrofit默认会使用OKHttp处理其他网络层请求。
  • 从Android4.4开始HttpURLConnection的底层实现采用的是okHttp.

用法

  1. 新建一个OkHttpClient对象
  2. 通过Request.Builder对象新建一个Request对象
  3. 返回执行结果

    • GET
<code class="hljs vbscript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String url) { 
        OkHttpClient client = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new OkHttpClient(); 
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Request <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Request.Builder() 
                .url(url) 
                .build(); 
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Response <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
        try { 
            <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response = client.newCall(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request).<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">execute(); 
            return <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response.body().<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
  • POST

POST需要使用RequestBody对象,之后再构建Request对象时调用post函数将其传入即可

<code class="hljs vbscript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String post(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String url) { 
        OkHttpClient client = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new OkHttpClient(); 
 
        RequestBody formBody = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new FormEncodingBuilder() 
                .add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user", <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Jurassic Park") 
                .add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"pass", <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"asasa") 
                .add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"time", <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"12132") 
                .build(); 
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Request <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Request.Builder() 
                .url(url) 
                .post(formBody) 
                .build(); 
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Response <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
        try { 
            <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response = client.newCall(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request).<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">execute(); 
            return <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response.body().<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul>

此外,post的使用方法还支持文件等操作,具体使用方法有兴趣的可以自行查阅

  • 对Gson的支持

okHttp还自带了对Gson的支持

<code class="hljs vbscript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private Person gson(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String url){ 
        OkHttpClient client = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new OkHttpClient(); 
        Gson gson = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new Gson(); 
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Request <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Request.Builder() 
                .url(url) 
                .build(); 
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Response <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
        try { 
            <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response = client.newCall(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request).<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">execute(); 
            Person person = gson.fromJson(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">response.body().charStream(), Person.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class); 
            return person; 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null; 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>
  • 异步操作

以上的两个例子必须在子线程中完成,同时okHttp还提供了异步的方法调用,通过使用回调来进行异步调用,然后okHttp的回调依然不在主线程中,因此该回调中不能操作UI

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">getAsync(String url) { 
        OkHttpClient client = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new OkHttpClient(); 
        Request request = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new Request.Builder() 
                .url(url) 
                .build(); 
        Response response = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
 
        client.newCall(request).enqueue(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new Callback() { 
            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onFailure(Request request, IOException e) { 
 
            } 
 
            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">onResponse(Response response) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws IOException { 
                String result = response.body().string(); 
                Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show(); 
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//不能操作ui,回调依然在子线程 
                Log.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TAG", result); 
            } 
        }); 
 
 
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>

okHttp的使用还有很多内容,这里也不过多介绍,更多内容,参考官方网址

Retrofit

特点

  1. 性能最好,处理最快
  2. 使用REST API时非常方便;
  3. 传输层默认就使用OkHttp;
  4. 支持NIO;
  5. 拥有出色的API文档和社区支持
  6. 速度上比volley更快;
  7. 如果你的应用程序中集成了OKHttp,Retrofit默认会使用OKHttp处理其他网络层请求。
  8. 默认使用Gson

使用

Retrofit支持同步和异步两种方式,在使用时,需要将请求地址转换为接口,通过注解来指定请求方法,请求参数,请求头,返回值等信息。还是使用之前的person的那段json值,get请求到服务器后从数据库查询数据,返回值为查询到的数据,post请求向服务器提交一条数据,返回值为提交的数据。 
首先完成请求所用的service,是一个interface,完全通过注解完成配置

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package cn.edu.zafu.http; 
 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import retrofit.Callback; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import retrofit.http.Field; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import retrofit.http.FormUrlEncoded; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import retrofit.http.GET; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import retrofit.http.Headers; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import retrofit.http.POST; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import retrofit.http.Path; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import retrofit.http.Query; 
 
<span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** 
 * Created by lizhangqu on 2015/5/11. 
 */ 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">interface <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">PersonService { 
    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Headers({ 
            <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Cache-Control: max-age=640000", 
            <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" 
    }) 
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//通过注解设置请求头 
    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@GET(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/{test}/rest.php") 
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置请求方法为get,相对路径为注解内内容,其中{test}会被@Path注解指定内容替换 
    Person getPerson(<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Path(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test") String dir,<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Query(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name") String name); 
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//@Query用于指定参数 
 
    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@FormUrlEncoded 
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//urlencode 
    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@POST(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/test/rest1.php") 
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//post提交 
    Person updatePerson(<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Field(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name") String name,<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Field(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age") <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int age); 
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//@Field提交的域 
 
 
    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@POST(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/test/rest1.php") 
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void updatePerson(<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Field(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name") String name,<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Field(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age") <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int age, Callback<Person> callback); 
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//异步回调,不能指定返回值 
} 
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li></ul>
  • GET 
    使用时,通过RestAdapter的实例获得一个接口的实例,其本质是动态代理,注意含有返回值的方法是同步的,不能UI线程中调用,应该在子线程中完成
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">RestAdapter restAdapter = new RestAdapter<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Builder() 
                        <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setEndpoint(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://121.41.119.107") 
                        <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.build()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
                PersonService personService=restAdapter<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.create(PersonService<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
                Person person=personService<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getPerson(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test",<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zhangsan")<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
                Log<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TAG",person<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.toString())<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
  • POST

POST的调用同Get,获得adapter后获得一个代理对象,然后通过这个代理对象进行网络请求

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">Person person1=personService<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.updatePerson(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lizhangqu", <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
Log<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TAG",person1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.toString())<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
  • 异步请求

如果要使用异步请求,需要将接口中的方法返回值修改会void,再加入回调参数Callback,就如PersonService中第三个方法一样,请求完成后会回调该callback对象的success或者fail方法。

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">RestAdapter restAdapter = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new RestAdapter.Builder() 
                .setEndpoint(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://121.41.119.107") 
                .build(); 
        PersonService personService=restAdapter.create(PersonService.class); 
        personService.updatePerson(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lizhangqu",<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new Callback<Person>() { 
            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">success(Person person, Response response) { 
                Log.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TAG", person.toString()); 
            } 
 
            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">failure(RetrofitError error) { 
 
            } 
        });</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

Retrofit的使用还有很多内容,剩下的就留给各位读者自行去发现了,而其官网页提供了及其详细的说明。下面提供官方网址

retrofit官网示例

这个库里面有很多精华的内容,建议各位仔细的阅读下官方的文档。

RoboSpice

见之前写的一篇博文

RoboSpice:android异步网络库简单用法

总结

网络请求库多种多样,最终其本质思想是一致的,要学会融汇贯通,还是要fucking the source code。由于本篇文章已经过长,所以图片的网络加载准备另开一篇博客进行整理。

源码下载

源码下载

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

netty对http协议解析原理解析详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。