android 网络请求封装,可改

/**
 * 创建一个回调接口,让需要做网络请求的Activity实现此接口
 */
public interface DataCallBack {
     void onCallBackSuccessed(int notify, String result);//请求响应成功时回调
     void onCallBackFailed();//请求响应失败时回调
}
/**
 * 网络请求工具类
 */
public class HttpRequest implements Constans {

    private String tag = "HttpRequest";
    private FinalHttp fh;

    private DataCallBack dataCallBack;
    private Context context;
    private MyToast myToast;
    private Handler okHttpHandler;
    public HttpRequest(DataCallBack dataCallBack, Context context) {
        fh = new FinalHttp();
        this.context = context;
        this.dataCallBack = dataCallBack;
        this.okHttpHandler=new Handler(Looper.getMainLooper());
        myToast = new MyToast(context);
    }

    public void requestPost(int notify, Map<String, String> map, String url) {
        requestData(notify, map, url, POST);
    }

    public void requestGet(int notify, Map<String, String> map, String url) {
        requestData(notify, map, url, GET);
    }

    //封装网络请求方法
    private void requestData(int notify, Map<String, String> map, String url, String requestMothed) {
        String httpUrl = url;
        if (requestMothed.equals("get")) {
//            //get请求
//            String urlParam = "";
//            if (!url.contains("?"))
//                httpUrl += "?";
//            else
//                httpUrl += "&";
//            int i = map.size();
//            for (String key : map.keySet()) {
//                i--;
//                urlParam += key + "=" + map.get(key);
//                if (i > 0) {
//                    urlParam += "&";
//                }
//            }
//            httpUrl += urlParam;
            AjaxParams ap = new AjaxParams(map);
            System.out.println(tag + ":get-httpUrl:" + httpUrl);
            System.out.println(tag + ":get-httpUrl-map:" + map);
            fh.configTimeout(10000);// 超时时间
            fh.get(httpUrl, ap, new HttpListener(notify, dataCallBack));
        } else {
            //post请求
            System.out.println(tag + ":post-httpUrl:" + httpUrl);
            System.out.println(tag + ":post-httpUrl-map:" + map);
//            fh.configTimeout(10000);// 超时时间
//            AjaxParams ap = new AjaxParams(map);
//            fh.post(httpUrl, ap, new HttpListener(notify, dataCallBack));
            OkHttpClient client = new OkHttpClient();
            //构建Request,解析链接,这里可选get/post方法
            FormBody.Builder builder = new FormBody.Builder();
            for (String key : map.keySet()) {
                builder.add(key, map.get(key));
            }
            final Request request = new Request.Builder().post(builder.build()).url(httpUrl).build();
            //添加到请求队列
            client.newCall(request).enqueue(new HttpListener1(notify, dataCallBack));

        }
    }


    class HttpListener1 implements okhttp3.Callback {

        private int notify;
        private DataCallBack dataCallBack;

        public HttpListener1(int notify, DataCallBack dataCallBack) {
            this.notify = notify;
            this.dataCallBack = dataCallBack;
        }


        @Override
        public void onFailure(Call call, IOException e) {
            // TODO Auto-generated method stub
            System.out.println(tag + ":" + e);
            //  myToast.showToast(context.getResources().getString(R.string.network_error), context);

            if (this.dataCallBack != null) {
                /** 执行onCallBackFailed()回调方法 */
                this.dataCallBack.onCallBackFailed();
            }
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final String responseStr = response.body().string();

            okHttpHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (dataCallBack != null) {
                        dataCallBack.onCallBackSuccessed(notify,responseStr);
                    }
                }
            });
        }
    }

    /**
     * Afinal回调
     */
    class HttpListener extends AjaxCallBack<String> {

        private int notify;
        private DataCallBack dataCallBack;

        public HttpListener(int notify, DataCallBack dataCallBack) {
            this.notify = notify;
            this.dataCallBack = dataCallBack;
        }

        @Override
        public void onSuccess(String t) {
            // TODO Auto-generated method stub
            System.out.println(tag + ":" + t);
            if (this.dataCallBack != null) {
                /** 执行onCallBackSuccessed()回调方法 */
                this.dataCallBack.onCallBackSuccessed(notify, t);
            }
        }

        @Override
        public void onFailure(Throwable t, String strMsg) {
            // TODO Auto-generated method stub
            System.out.println(tag + ":" + strMsg);
            myToast.showToast(context.getResources().getString(R.string.network_error), context);
            if (this.dataCallBack != null) {
                /** 执行onCallBackFailed()回调方法 */
                this.dataCallBack.onCallBackFailed();
            }
        }
    }
}
/**
 * 获取网络数据类
 */
public class GetNetData implements Constans {
    private HttpRequest httpRequest;

    private Long mTime; // 时间
    private String mKey = "bxapp"; // key
    private String sign;
    private String time;

    public GetNetData(DataCallBack dataCallBack,Context context) {
        this.httpRequest = new HttpRequest(dataCallBack,context);

    }

    //获取登录接口请求方法
    public void getLogin(int notify, String tel, String pwd,String devid) {
        Map<String, String> map = new HashMap<String, String>();
        mTime = System.currentTimeMillis() / 1000;
        time = WaMD5.getMD5(mTime.toString());
        sign = WaMD5.getMD5(time + mKey);
        map.put("tel", tel);
        map.put("pwd", pwd);
        map.put("timestamp", mTime.toString());
        map.put("sign", sign);
        map.put("devid", devid);
        map.put("devt", "android");
        httpRequest.requestPost(notify, map, WA_LOGIN_URL);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值