您的位置:首页 > 理论基础 > 计算机网络

使用异步HttpClient框架提交数据

2016-03-08 15:34 531 查看
android-async-http框架是一个开源的Android异步网络请求处理库,网络处理均基于Android的非UI线程,通过回调方法处理请求结果,可以轻松的获取网络数据或者向服务器发送数据,使用起来非常简单。

其主要特征如下:处理异步Http请求,并通过匿名内部类处理回调结果,Http异步请求均位于非UI线程,不会阻塞UI操作,通过线程池处理并发请求处理文件上传、下载,响应结果自动打包JSON格式.自动处理连接断开时请求重连.

项目托管在Github,详细内容请访问:https://github.com/loopj/android-async-http 下载jar包和开发文档等,然后将jar包引入我们自己的项目的libs目录中。

特性:

Features

Make asynchronous HTTP requests, handle responses in anonymous callbacks

HTTP requests happen outside the UI thread

Requests use a threadpool to cap concurrent resource usage

GET/POST params builder (RequestParams)

Multipart file uploads with no additional third party libraries

Streamed JSON uploads with no additional libraries

Handling circular and relative redirects

Tiny size overhead to your application, only 90kb for everything

Automatic smart request retries optimized for spotty mobile connections

Automatic gzip response decoding support for super-fast requests

Binary protocol communication with BinaryHttpResponseHandler

Built-in response parsing into JSON with JsonHttpResponseHandler

Saving response directly into file with FileAsyncHttpResponseHandler

Persistent cookie store, saves cookies into your app’s SharedPreferences

Integration with Jackson JSON, Gson or other JSON (de)serializing libraries with BaseJsonHttpResponseHandler

Support for SAX parser with SaxAsyncHttpResponseHandler

Support for languages and content encodings, not just UTF-8

官方建议使用一个静态的AsyncHttpClient:

public class HttpClientUtils {    
    private static String sessionId = null;
    private static AsyncHttpClient client = new AsyncHttpClient();   
    private static PersistentCookieStore cookieStore ; 
    static {    
        //设置网络超时时间
         client.setTimeout(5000); 
    }    
    public static void get(String url, AsyncHttpResponseHandler responseHandler) { 
        client.get(url, responseHandler);
    }    
    public static void get(Context context,String url,ResponseHandlerInterface responseHandler) { 
        client.get(context, url, responseHandler);
    }  
    public static void get(String url,RequestParams params, ResponseHandlerInterface responseHandler) {  
        client.get(url, params, responseHandler);    } 
    public static void get(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler) {
        client.get(context, url, params, responseHandler);    
    } 
    public static void get(Context context, String url, Header[] headers, RequestParams params, ResponseHandlerInterface responseHandler) {
        client.get(context, url, headers, params, responseHandler); 
    }  
    public static void post(String url,RequestParams params, ResponseHandlerInterface responseHandler){  
        client.post(url, params, responseHandler);
    }
    public static AsyncHttpClient getClient(){    
        return client;
    }
    public static String getSessionId() {    
        return sessionId; 
    }
    public static void setSessionId(String sessionId) {   
        HttpClientUtils.sessionId = sessionId; 
    } 
    public static PersistentCookieStore getCookieStore() {    
        return cookieStore;  
    }
    public static void setCookieStore(PersistentCookieStore cookieStore) { 
        HttpClientUtils.cookieStore = cookieStore;   
        client.setCookieStore(cookieStore); 
    }
}

简单的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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">HttpClientUtils.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://www.baidu.com"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> AsyncHttpResponseHandler() {
     @Override
     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onSuccess</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> statusCode, Header[] headers, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] responseBody) {
          System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(response);
     }
     @Override
     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onFailure</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> statusCode, Header[] headers, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] responseBody, Throwable
 error)
 {
          error.printStackTrace(System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>);
     }
 });</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></ul>

带参数get请求

//使用RequestParams类携带数据
<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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">RequestParams <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> RequestParams();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span>.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value1"</span>, value1);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span>.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value2"</span>, value2);
HttpClientUtils.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get</span>(url, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JsonHttpResponseHandler(){
                @Override
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onSuccess</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> statusCode, Header[] headers,
                        JSONObject response) {
                    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//请求成功回调</span>
                }
                @Override
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onFinish</span>() { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//请求完成</span>
                }
                @Override
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onFailure</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> statusCode, Header[] headers,
                        String responseString, Throwable throwable) {
                    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//请求失败</span>
                }

            });</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></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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">RequestParams <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> RequestParams();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span>.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value1"</span>, value1);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span>.put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value2"</span>, value2);
HttpClientUtils.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get</span>(url, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JsonHttpResponseHandler(){
                @Override
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onSuccess</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> statusCode, Header[] headers,
                        JSONObject response) {
                    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//请求成功回调</span>
                }
                @Override
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onFinish</span>() { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//请求完成</span>
                }
                @Override
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onFailure</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> statusCode, Header[] headers,
                        String responseString, Throwable throwable) {
                    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//请求失败</span>
                }

            });</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></ul>
今天就先简单介绍android-async-http的基本使用,android-async-http开源框架还有很多方法没有介绍,在稍后的文章再深入讲解,读者也可以自己去探索android-async-http的更多功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: