async-http 的封装

来源:互联网 发布:绝食知乎 编辑:程序博客网 时间:2024/06/06 08:52

真心希望每一位程序员不要人云亦云,把代码测试过之后再分享出来。

近期想写个应用,经网友分享,好像asynchttp不错,为了省事我也拿来一用。我用的是1.4.9版本,开始感觉还是不错的。

compile 'com.loopj.android:android-async-http:1.4.9'

由于应用与服务器交互太多,导致代码量过多,官方给出的封装不能满足我的需要,所以我自己封装了一个。代码如下:

/** * Created by hhc on 2017/2/4. * 由于有对话框,需要传入Activity. * hhcloading为我自己封装的一个系统等待类,代码另行贴出 */public abstract class HhcAsynRequest {    private static final String BASE_URL = "http://API地址";    private static AsyncHttpClient client = new AsyncHttpClient();    private Hhcloading hhcloading;    private Activity activity;    private static String getAbsoluteUrl(String relativeUrl) {        return BASE_URL + relativeUrl;    }    public HhcAsynRequest(Activity activity,String type, String subUrl, RequestParams params) {        this.activity = activity;        request(type, subUrl, params);    }    public void request(String type, String subUrl, RequestParams params){        hhcloading = Hhcloading.showDialog(activity);        hhcloading.show();        if (type.equals("get")){            client.get(getAbsoluteUrl(subUrl), params, new AsyncHttpResponseHandler(){                @Override                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {                    try {                        String json = new String(responseBody);                        JSONObject response = new JSONObject(json);                        analyzeJson(statusCode, headers,response);                    }catch (Exception e){                        Toast.makeText(ContextHolder.getContext(),"数据错误(901):数据格式无效。",Toast.LENGTH_LONG).show();                    }                /*隐藏等待对话框*/                    if (hhcloading != null) {                        hhcloading.dismiss();                    }                }                @Override                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {                /*隐藏等待对话框*/                    if (hhcloading != null) {                        hhcloading.dismiss();                    }                    Toast.makeText(ContextHolder.getContext(),"网络错误(900):无法连接到服务器。",Toast.LENGTH_LONG).show();                }            });        }else {            client.post(getAbsoluteUrl(subUrl), params, new AsyncHttpResponseHandler(){                @Override                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {                    try {                        String json = new String(responseBody);                        JSONObject response = new JSONObject(json);                        analyzeJson(statusCode, headers,response);                    }catch (Exception e){                        Toast.makeText(ContextHolder.getContext(),"数据错误(901):数据格式无效。",Toast.LENGTH_LONG).show();                    }                /*隐藏等待对话框*/                    if (hhcloading != null) {                        hhcloading.dismiss();                    }                }                @Override                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {                /*隐藏等待对话框*/                    if (hhcloading != null) {                        hhcloading.dismiss();                    }                    Toast.makeText(ContextHolder.getContext(),"网络错误(900):无法连接到服务器。",Toast.LENGTH_LONG).show();                }            });        }    }    /*对外接口*/    public abstract void analyzeJson(int statusCode, Header[] headers, JSONObject response);}

这样,在与服务器交互的时候,只要把API交互地址及提交数据写入就可以了,加载等待,错误处理等千篇一律的事情统一处理一下就可以了。

坑:在使用官方封装的时候,直接使用了返回JSON数据的方式,结果入坑。

“坑”占位》》

0 0
原创粉丝点击