android 网络请求封装,可改
来源:互联网 发布:linux 方向 编辑:程序博客网 时间:2024/06/02 02:13
/** * 创建一个回调接口,让需要做网络请求的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
- android 网络请求封装,可改
- android封装网络请求
- Android Http网络请求封装
- Android网络请求代码封装
- android Xutils3网络请求封装
- Android网络请求简单封装
- Android封装OkHttp网络请求
- Android网络请求--动态加载的效果(已封装、可直接使用)
- Android-封装post和get 网络请求
- Android网络请求框架AsyncHttp二次封装
- Android网络请求HttpUtil封装工具类
- 常见的Android 网络请求的封装;
- Android中okhttp网络请求的封装
- 自己封装了一个Okhttp网络请求,操作简单,可区分一个类中的不同请求
- 封装网络请求
- 封装Ger网络请求
- 网络请求封装
- 封装网络请求(AFNetworking)
- LeetCode 153 Find Minimum in Rotated Sorted Array
- Oracle系统包——dbms_alert用法
- 踩坑总结
- ldd3 pipe示例
- C++之栈的实现
- android 网络请求封装,可改
- java、sftp文件服务器的操作(获取链接。上传、下载,删除,获取图片或者文件)
- Ubuntu14.04下将java1.7升级到java1.8版本
- match_parent fill_parent和wrap_parent的区别
- c结构体数组的赋值语法细节
- InfluxDB
- [HDU] 1010 Tempter of the Bone [DFS][奇偶剪枝]
- c/c++ struct的定义、声明、对齐方式
- ruby中的变量及其作用域