赞
踩
近期干了一件非常蛋疼的事情,就是在整个项目的数据请求中添加参数,实现起来倒是简单,但是项目中的涉及到的网络请求没有封装,导致要改的话就要所有的网络请求以及接口都是要改动的,真心是无语。没办法,当时刚接手这个项目的时候就想过要封装一个网络请求的框架,但是又觉得工作量太大,而且自己擅自改动后还要再走测试流程也就想想就放弃了。没想到到今天还是要动工,哎没办法只能加班加点。
先给大家展现下原来的接口请求是什么样的:
http://192.168.2.157:9090/getAdConfigureList?body={"params":{"terminalCode":"868403025191716|1|6.0|20170605154050","merchantId":"1","version":"25"}}
这是项目中的原来的接口,现在的需求是这样的:
- http://192.168.2.157:9090/getAdConfigureList?
- monitor={"userId":"8765009300000000065","token":"1545471319684"}
- &body={"params":{"terminalCode":"868403025191716|1|6.0|20170605154050","merchantId":"1","version":"25"}}
其实很简单:
- private void postGetPaymentChannel() {
- String str = BaseActivity.urlString + "getPaymentChannel";
-
- Map<String, Object> mapStart = new HashMap<String, Object>();
- mapStart.put("token", BaseActivity.baseParams().get(BaseActivity.PRE_KEY_TOKEN) + "");
- mapStart.put("userId", BaseActivity.baseParams().get(BaseActivity.PRE_KEY_USER_ID)+ "");
- JSONObject mapStartJson = new JSONObject(mapStart);
- RequestParams params = new RequestParams();
- params.put("body", mapStartJson.toString());
-
- AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
- asyncHttpClient.post(str, params, new AsyncHttpResponseHandler() {
-
- @Override
- public void onFailure(Throwable error, String content) {
- super.onFailure(error, content);
- }
- @Override
- public void onStart() {
- super.onStart();
- }
- @Override
- public void onSuccess(String content) {}
- });
- }
原来的方法里面只放了body,那么现在要求放入monitor,可以这样做:
- private void postGetPaymentChannel() {
- String str = BaseActivity.urlString + "getPaymentChannel";
-
- Map<String, Object> mapStart = new HashMap<String, Object>();
- mapStart.put("token", BaseActivity.baseParams().get(BaseActivity.PRE_KEY_TOKEN) + "");
- mapStart.put("userId", BaseActivity.baseParams().get(BaseActivity.PRE_KEY_USER_ID)+ "");
- JSONObject mapStartJson = new JSONObject(mapStart);
- RequestParams params = new RequestParams();
- params.put("body", mapStartJson.toString());
-
- Map<String, Object> mapEnd = new HashMap<String, Object>();
- mapEnd.put("token", BaseActivity.baseParams().get(BaseActivity.PRE_KEY_TOKEN) + "");
- mapEnd.put("userId", BaseActivity.baseParams().get(BaseActivity.PRE_KEY_USER_ID)+ "");
- JSONObject jsonlAST = new JSONObject(mapEnd);
- params.put("monitor", jsonlAST.toString());
-
- AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
- asyncHttpClient.post(str, params, new AsyncHttpResponseHandler() {
-
- @Override
- public void onFailure(Throwable error, String content) {
- super.onFailure(error, content);
- }
- @Override
- public void onStart() {
- super.onStart();
- }
- @Override
- public void onSuccess(String content) {}
- });
- }
其实值改一个接口的话这样做确实是很简单的,但是项目中这么多的接口都要修改怎么办呢?
这一次没办法,只能一个个找到一个个改,单这一次必须吸取教训,抽出来,然后下次需要加参数的时候就可以只动一个地方就ok了,怎么抽呢?
- public class HttpClientPostUtils {
- private static String TAG = "postData";
- /**
- *
- * @param mapHead
- * body里面的参数
- * @param mapEnd
- * 后面追加的一个body
- * @param tag
- * 打印日志用的标志
- * @param headUrl
- * 请求的地址头
- * @param internetResultListener
- * 请求的接口回调
- */
- public static void postForDatas(Map<String, Object> mapHead, String tag, String headUrl,
- final InternetResultListener internetResultListener) {
- String urlStr = BaseActivity.urlString + headUrl;
- RequestParams params = new RequestParams();
- Map<String, Object> paramsMap = new HashMap<String, Object>();
- paramsMap.put("params", mapHead);
- JSONObject json = new JSONObject(paramsMap);
- params.put("body", json.toString());
- Map<String, Object> mapEnd = new HashMap<String, Object>();
- mapEnd.put("token", BaseActivity.baseParams().get(BaseActivity.PRE_KEY_TOKEN) + "");
- mapEnd.put("userId", BaseActivity.baseParams().get(BaseActivity.PRE_KEY_USER_ID) + "");
- JSONObject jsonlAST = new JSONObject(mapEnd);
- params.put("monitor", jsonlAST.toString());
-
- Logger.i(TAG, tag + urlStr + "?" + params.toString());
- AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
- asyncHttpClient.post(urlStr, params, new AsyncHttpResponseHandler() {
-
- @Override
- public void onStart() {
- super.onStart();
- internetResultListener.onPostStart();
- }
-
- @Override
- public void onSuccess(int statusCode, String content) {
- super.onSuccess(statusCode, content);
- if (200 == statusCode) {
- internetResultListener.onPostSuccess(statusCode, content);
- } else {
- Throwable error = new Throwable("失败了");
- internetResultListener.onPostFailure(error, "状态码 = " + statusCode + "content = " + content);
- }
- }
- @Override
- public void onFailure(Throwable error, String content) {
- super.onFailure(error, content);
- internetResultListener.onPostFailure(error, content);
- }
-
- });
- }
-
- }
其中回调的listener:
- public interface InternetResultListener {
- void onPostStart();
- void onPostFailure(Throwable error, String content);
- void onPostSuccess(int statusCode, String content);
- }
- public void Check_login_Post(String userId, String token, final int type) {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("userId", userId);
- map.put("token", token);
- String headUrl = "seachToken";
- HttpClientPostUtils.postForDatas(map, "检查是被登录接口", headUrl, new InternetResultListener() {
-
- @Override
- public void onPostSuccess(int statusCode, String content) {
-
- //成功的回调
- }
-
- @Override
- public void onPostStart() {
- //开始的回调
- }
-
- @Override
- public void onPostFailure(Throwable error, String content) {
- //失败的回调
- }
- });
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。