Android链式结构封装

相关文章:《Okttp3.0简单的二次回调封装

在java面向对象语言中,一贯喜欢采用函数式编程,把一个复杂的逻辑分解成几个函数来相互调用来达到自己的业务逻辑,一直用过okhttp的朋友能有不知道有没有对okhttp的链式结构感兴趣呢?今天没事,试着写了一下。

拿我们最熟悉的AsyncHttpClient这个网络请求框架做例子:

一、AsyncHttpClient的常规用法

AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("loginPassword","18058810112");
params.put("loginAccount","123456");
client.post(url,params,new AsyncHttpResponseHandler(){
	@Override
	public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody) {
		super.onSuccess(statusCode, headers, responseBody);
		Toast.makeText(MainActivity.this,new String(responseBody),Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onFailure(int statusCode, org.apache.http.Header[] headers, byte[] responseBody, Throwable error) {
		super.onFailure(statusCode, headers, responseBody, error);
		Toast.makeText(MainActivity.this,"失败",Toast.LENGTH_SHORT).show();
	}
});
二、函数式封装

/**
 * AsyncHttp的Post请求
 *
 * @param context
 * @param url
 * @param params
 * @param httpCallBack
 */
public static void AsyncHttpPost(final Context context, String url, RequestParams params, final HttpCallBackInterface httpCallBack) {
	if (AppUtil.isNetworkConnected(context)) {
		SingletonsManager.getAsyncHttpInstance().post(context, url, params, new AsyncHttpResponseHandler() {
			@Override
			public void onStart() {
				super.onStart();
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
				httpCallBack.OnSuccess(new String(responseBody));
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
				AppUtil.showToast(context, "网络链接失败!");
			}

			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	} else {
		AppUtil.showToast(context, "当前无网络可用!");
	}
}
看一下链式结构封装

三、仿okhttp链式结构封装:

(1)创建请求成功的回掉接口(只实现请求成功的回调)

public interface AsyncHttpCallback {
    public void OnSucess(String result);
}

(2)创建封装类

public class AsyncHttpManager {
    private String url;
    private static AsyncHttpManager mAsyncHttpManager;
    private static AsyncHttpClient mClient;
    private static RequestParams mParams;

    //私有的构造方法,防止随意创建实例
    private AsyncHttpManager(){

    }

    /**
     * newBuild创建实例
     *
     * @return mAsyncHttpManager 
     * 返回一定是AsyncHttpManager本身,这样才能继续调用别的方法,也就是所谓的链式结构
     */
    public static AsyncHttpManager newBuild(){
        mAsyncHttpManager = new AsyncHttpManager();
        mClient = new AsyncHttpClient();
        mParams = new RequestParams();
        return mAsyncHttpManager;
    }

    public AsyncHttpManager url(String url){
        this.url = url;
        return this;
    }

    public AsyncHttpManager addParamter(String key,String value){
        mParams.put(key,value);
        return this;
    }
    public void callBack(final AsyncHttpCallback asyncHttpCallback){
        AsyncHttpClient client = new AsyncHttpClient();
        client.post(url,mParams,new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                super.onSuccess(statusCode, headers, responseBody);
                //调用回调接口OnSucess自定义实现
                asyncHttpCallback.OnSucess(new String(responseBody));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                super.onFailure(statusCode, headers, responseBody, error);
            }
        });
    }
}

(3)activity中的调用

AsyncHttpManager.newBuild()
	.url(url)
	.addParamter("loginPassword","18058810112")
	.addParamter("loginAccount","123456")
	.callBack(new AsyncHttpCallback() {
		@Override
		public void OnSucess(String result) {
			Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
		}
	});
简单的思路,有什么需求直接添加方法就ok。

通过抓包可以看出,这就是我们所要实现的效果了

Android链式结构封装_第1张图片




你可能感兴趣的:(Android)