android 網路請求封裝,可改
阿新 • • 發佈:2019-02-08
/** * 建立一個回撥介面,讓需要做網路請求的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); }
}