今天再对android-async-http简单封装封装一下,同样也只是拿来练手……
1.首先创建AsyncHttpUtilClient类
/**
* 再封装android-async-http
*
* @author wen
* @version 2015年1月19日
* @see AsyncHttpUtilClient
* @since
*/
public class AsyncHttpUtilClient {
private final static String TAG = "AsyncHttpUtilClient";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void doGet(int method, String url, String headerValue,
RequestParams params, AsyncHttpResponseHandler responseHandler) {
if (null != headerValue) {
// 设置请求头参数 client.addHeader("XX", headerValue);
}
client.get(url, params, responseHandler);
}
public static void doPost(int method, String url, String headerValue,
RequestParams params, AsyncHttpResponseHandler responseHandler) {
if (null != headerValue) {
// 设置请求头参数 client.addHeader("XX", headerValue);
}
client.post(url, params, responseHandler);
}
public static void doPut(int method, String url, String headerValue,
RequestParams params, AsyncHttpResponseHandler responseHandler) {
if (null != headerValue) {
// 设置请求头参数 client.addHeader("XX", headerValue);
}
client.put(url, params, responseHandler);
}
public static void request(AsyncBean asyncBean,
AsyncHttpResponseHandler responseHandler) {
if (NetUtils.netIsAvailable()) {
switch (asyncBean.getMethod()) {
case Constant.GET:
doGet(asyncBean.getMethod(), asyncBean.getUrl(),
asyncBean.getHeaderValueString(),
asyncBean.getParams(), responseHandler);
break;
case Constant.POST:
doPost(asyncBean.getMethod(), asyncBean.getUrl(),
asyncBean.getHeaderValueString(),
asyncBean.getParams(), responseHandler);
break;
case Constant.PUT:
doPut(asyncBean.getMethod(), asyncBean.getUrl(),
asyncBean.getHeaderValueString(),
asyncBean.getParams(), responseHandler);
break;
case Constant.DELETE:
//
break;
}
}
}
}
2.创建AsyncRequestParam类
public class AsyncRequestParam {
// 这里只是一个例子
public static RequestParams setLoginParam(String name, String pwd,
String client) {
RequestParams params = new RequestParams();
params.put("login_name", name);
params.put("password", pwd);
return params;
}
}
3.创建CreateAsyncConnectBean类
public class CreateAsyncConnectBean {
private final static String TAG = "CreateAsyncConnectBean";
private static CreateAsyncConnectBean createAsyncB = new CreateAsyncConnectBean();
private static AsyncBean asyncBean = new AsyncBean();
public static AsyncBean getLoginCB(RequestParams params) {
asyncBean.setMethod(Constant.POST); // 设置请求方式
asyncBean.setUrl("your url"); // 设置请求地址
asyncBean.setHeaderValueString(null); // 设置请求头
asyncBean.setParams(params); // 设置请参数
return asyncBean;
}
}
public class AsyncBean {
int method;
String url;
String headerValueString;
RequestParams params;
public AsyncBean() {
super();
}
public AsyncBean(int method, String url, String headerValueString,
RequestParams params) {
super();
this.method = method;
this.url = url;
this.headerValueString = headerValueString;
this.params = params;
}
public int getMethod() {
return method;
}
public void setMethod(int method) {
this.method = method;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getHeaderValueString() {
return headerValueString;
}
public void setHeaderValueString(String headerValueString) {
this.headerValueString = headerValueString;
}
public RequestParams getParams() {
return params;
}
public void setParams(RequestParams params) {
this.params = params;
}
}
4.最后,只需要在你需要的地方写这么几行代码就OK了
RequestParams loginParams = AsyncRequestParam.setLoginParam(
"name", "pwd");
AsyncHttpUtilClient.request(
CreateAsyncConnectBean.getLoginCB(loginParams),
new AsyncHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable arg3) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
String s = new String(responseBody);
L.i(TAG, s);
}
});