Android android-async-http简单再封装

今天再对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);

					}

				});






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值