/** * 创建一个回调接口,让需要做网络请求的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); }}
android 网络请求封装,可改
最新推荐文章于 2021-05-26 07:30:59 发布