转载请注明出处: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