Android - AsyncHttpClient - Sử dụng thư viện AsyncHtttpClient trong Android Studio
1. Tổng quan
2. Sử dụng AsyncHttpClient
Bước 1: Thêm dependency trong build.grade
com.loopj.android:android-async-http
com.google.code.gson:gson
static final String BASE_URL = "URL here";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void post(String url, RequestParams params, AsyncHttpResponseHandler
responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
public class DataAsyncHttpManager {
private DataAsyncHttpListener dataAsyncHttpListener;
public interface DataAsyncHttpListener {
void commitDataAsyncHttp(Data data);
void onErrorDataAsyncHttp();
}
public void registerListener(DataAsyncHttpListener dataAsyncHttpCallback) {
this.dataAsyncHttpListener = dataAsyncHttpCallback;
}
public void unregisterListener() {
this.dataAsyncHttpListener = null;
}
public void postData(int numberProduct) {
final RequestParams requestParams = new RequestParams();
requestParams.put("param_key", "param_value");
DataAsyncAPI.post("path", requestParams, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
Gson gson = new Gson();
DataModel dataModel = gson.fromJson(response.toString(), DataModel.class);
dataAsyncHttpListener.commitDataAsyncHttp(dataModel.getData());
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
dataAsyncHttpListener.onErrorDataAsyncHttp();
}
});
}
// main activity
DataAsyncHttpManager dataAsyncHttpManager;
dataAsyncHttpManager = new DataAsyncHttpManager();
dataAsyncHttpManager.registerListener(this);
Chú ý với content type: requestParams.setForceMultipartEntityContentType(false);
2. Sử dụng AsyncHttpClient
Bước 1: Thêm dependency trong build.grade
com.loopj.android:android-async-http
com.google.code.gson:gson
Bước 2: Tạo client API
Bước 3: Tạo một Adapter class để sử dụng
public class DataAsyncAPI {static final String BASE_URL = "URL here";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void post(String url, RequestParams params, AsyncHttpResponseHandler
responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
public class DataAsyncHttpManager {
private DataAsyncHttpListener dataAsyncHttpListener;
public interface DataAsyncHttpListener {
void commitDataAsyncHttp(Data data);
void onErrorDataAsyncHttp();
}
public void registerListener(DataAsyncHttpListener dataAsyncHttpCallback) {
this.dataAsyncHttpListener = dataAsyncHttpCallback;
}
public void unregisterListener() {
this.dataAsyncHttpListener = null;
}
public void postData(int numberProduct) {
final RequestParams requestParams = new RequestParams();
requestParams.put("param_key", "param_value");
DataAsyncAPI.post("path", requestParams, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
Gson gson = new Gson();
DataModel dataModel = gson.fromJson(response.toString(), DataModel.class);
dataAsyncHttpListener.commitDataAsyncHttp(dataModel.getData());
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
dataAsyncHttpListener.onErrorDataAsyncHttp();
}
});
}
// main activity
DataAsyncHttpManager dataAsyncHttpManager;
dataAsyncHttpManager = new DataAsyncHttpManager();
dataAsyncHttpManager.registerListener(this);
Chú ý với content type: requestParams.setForceMultipartEntityContentType(false);
Nhận xét
Đăng nhận xét