AsyncHttpClient 开源框架學習研究

本文介绍了AsyncHttpClient库的使用,该库基于Apache HttpClient,提供异步HTTP请求功能。请求在子线程中执行,回调在主线程。内容包括基本用法、保存Cookie、带参数请求、上传文件及下载二进制数据的示例,以及HTTPS安全连接的配置。
摘要由CSDN通过智能技术生成

转载请注明出处:http://blog.csdn.net/krislight 

OverView:

AsyncHttpClient庫 基於Apache的HttpClient框架,是一個異步的httpClient, 所有的http請求都在子線程中,但是callback執行的線程和創建這個callback的線程是同一個(也即主線程創建的callback那麼執行的時候也是在主線程中)

基本用法:  

AsyncHttpClient client = new AsyncHttpClient();
	client.get("http://www.google.com", new AsyncHttpResponseHandler() {
	          @Override
			public void onStart() {
				super.onStart();
                    //in MainThread, you can do some ui operation here like progressBar.  
			}
			
			@Override
			public void onFinish() {
                    // no matter success or failed this method is always invoke
				super.onFinish();
			}
			
			@Override
			public void onSuccess(String content) {
                   //success
			}
			
			@Override
			public void onFailure(Throwable error, String content) {
                  //failed
			}	
});

項目中建議定義成靜態工具類:                   

public class TwitterRestClient {
		  private static final String BASE_URL = "http://api.twitter.com/1/";
		  private static AsyncHttpClient client = new AsyncHttpClient();
		  public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		      client.get(getAbsoluteUrl(url), params, responseHandler);
		  }
		  public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseH
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值