From 535d69817e83737f3da6250fc6fb70da25fc1a4c Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期四, 24 十一月 2022 12:11:36 +0800 Subject: [PATCH] 完善了功能 --- AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpSend.java | 176 ++++++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 115 insertions(+), 61 deletions(-) diff --git a/AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpSend.java b/AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpSend.java index e2317b7..12a46fc 100644 --- a/AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpSend.java +++ b/AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpSend.java @@ -1,17 +1,24 @@ package com.mm.android.deviceaddmodule.openapi; +import android.text.TextUtils; + import com.google.gson.Gson; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; +import com.mm.android.deviceaddmodule.LCDeviceEngine; import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.BusinessException; import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil; import com.mm.android.deviceaddmodule.mobilecommon.utils.MD5Helper; import org.apache.http.conn.ConnectTimeoutException; +import org.json.JSONObject; import java.io.IOException; import java.net.SocketException; import java.net.SocketTimeoutException; +import java.net.URLDecoder; import java.net.UnknownHostException; import java.net.UnknownServiceException; import java.util.HashMap; @@ -26,13 +33,17 @@ public static JsonObject execute(Map<String, Object> paramsMap, String method,int timeOut) throws BusinessException { Map<String, Object> map = paramsInit(paramsMap); - // 杩斿洖json - JsonObject jsonObj = doPost(CONST.HOST + "/openapi/" + method, map,timeOut); - LogUtil.debugLog(TAG, "url::"+method+"\n"+"response result锛�" + jsonObj.toString()); - if (jsonObj == null) { - throw new BusinessException("openApi response is null"); + JsonObject jsonResult = null; + if (CONST.isHdlServer()) { + jsonResult = doPost(CONST.HOST + "/home-wisdom/imou/openapi/" + method, map, timeOut); + } else { + jsonResult = doPost(CONST.HOST + "/openapi/" + method, map, timeOut); + if (jsonResult == null) { + throw new BusinessException("openApi response is null"); + } + jsonResult = jsonResult.getAsJsonObject("result"); } - JsonObject jsonResult = jsonObj.getAsJsonObject("result"); + if (jsonResult == null) { throw new BusinessException("openApi response is null"); } @@ -43,8 +54,8 @@ } try { JsonObject jsonData = jsonResult.getAsJsonObject("data"); - if (jsonData ==null){ - jsonData=new JsonObject(); + if (jsonData == null) { + jsonData = new JsonObject(); } return jsonData; } catch (Throwable e) { @@ -53,42 +64,41 @@ } } - public static JsonObject execute(String json, String method,int timeOut) throws BusinessException { - // 杩斿洖json - LogUtil.debugLog(TAG, "request result锛�" + json.toString()); - JsonObject jsonObj = doPost(CONST.HOST + "/openapi/" + method, json,timeOut); - LogUtil.debugLog(TAG, "URL::"+method+"\n"+"response result锛�" + jsonObj.toString()); - if (jsonObj == null) { - throw new BusinessException("openApi response is null"); - } - JsonObject jsonResult = jsonObj.getAsJsonObject("result"); - if (jsonResult == null) { - throw new BusinessException("openApi response is null"); - } - String code = jsonResult.get("code").getAsString(); - if (!"0".equals(code)) { - String msg = jsonResult.get("msg").getAsString(); - throw new BusinessException(code + msg); - } - try { - JsonObject jsonData = jsonResult.getAsJsonObject("data"); - if (jsonData ==null){ - jsonData=new JsonObject(); - } - return jsonData; - } catch (Throwable e) { - BusinessException businessException = new BusinessException(e); - throw businessException; - } - } + private static JsonObject doPost(String url, Map<String, Object> map, int timeOut) throws BusinessException { Gson gson = new Gson(); - String json = gson.toJson(map); - JsonObject jsonObject = new JsonObject(); + String json; + JsonObject jsonObject; + if(CONST.isHdlServer()) { + map.put("homeId", LCDeviceEngine.newInstance().getHomeId()); + if (map.containsKey("params")) { + Map<String, Object> tempMap = (Map<String, Object>) map.get("params"); + map.remove("params"); + map.remove("id"); + map.remove("system"); + for (Map.Entry<String, Object> temp : tempMap.entrySet()) { + if ("page".equals(temp.getKey())) { + map.put("pageNo", temp.getValue()); + } + else { + if("token".equals(temp.getKey())){ + continue; + } + map.put(temp.getKey(), temp.getValue()); + } + } + } + + json = gson.toJson(HttpClient.encrypt(map)); + }else{ + json = gson.toJson(map); + } + try { - LogUtil.debugLog(TAG, "reqest: " + url + " data:" + json.toString()); + LogUtil.debugLog(TAG, "reqest: " + url + " data:" + json); String openApi = HttpClient.post(url, json, "application/json", "OpenApi",timeOut); + LogUtil.debugLog(TAG, "杩斿洖缁撴灉: \r\n" + openApi); jsonObject = new JsonParser().parse(openApi).getAsJsonObject(); } catch (IOException e) { BusinessException b = new BusinessException(e); @@ -106,28 +116,6 @@ return jsonObject; } - private static JsonObject doPost(String url, String json, int timeOut) throws BusinessException { - Gson gson = new Gson(); - JsonObject jsonObject = new JsonObject(); - try { - LogUtil.debugLog(TAG, "reqest: " + url + " data:" + json.toString()); - String openApi = HttpClient.post(url, json, "application/json", "OpenApi",timeOut); - jsonObject = new JsonParser().parse(openApi).getAsJsonObject(); - } catch (IOException e) { - BusinessException b = new BusinessException(e); - if (e instanceof ConnectTimeoutException || e instanceof SocketTimeoutException - || e instanceof UnknownHostException - || e instanceof UnknownServiceException || e instanceof SSLException - || e instanceof SocketException) { - b.errorDescription = "鎿嶄綔澶辫触锛岃妫�鏌ョ綉缁�"; - } - throw b; - } catch (Throwable e) { - BusinessException b = new BusinessException(e); - throw b; - } - return jsonObject; - } private static Map<String, Object> paramsInit(Map<String, Object> paramsMap) { long time = System.currentTimeMillis() / 1000; @@ -152,4 +140,70 @@ map.put("id", id); return map; } + +// public static JsonObject execute(String json, String method,int timeOut) throws BusinessException { +// // 杩斿洖json +// LogUtil.debugLog(TAG, "request result锛�" + json.toString()); +// JsonObject jsonResult=null; +// if (CONST.isHdlServer()) { +// JsonObject jsonObj = doPost(CONST.HOST + "/home-wisdom/imou/openapi/" + method, json, timeOut); +// LogUtil.debugLog(TAG, "URL::"+method+"\n"+"response result锛�" + jsonObj.toString()); +// if (jsonObj == null) { +// throw new BusinessException("openApi response is null"); +// } +// jsonResult = jsonObj; +// } +// else { +// JsonObject jsonObj = doPost(CONST.HOST + "/openapi/" + method, json, timeOut); +// LogUtil.debugLog(TAG, "URL::"+method+"\n"+"response result锛�" + jsonObj.toString()); +// if (jsonObj == null) { +// throw new BusinessException("openApi response is null"); +// } +// jsonResult = jsonObj.getAsJsonObject("result"); +// } +// +// if (jsonResult == null) { +// throw new BusinessException("openApi response is null"); +// } +// String code = jsonResult.get("code").getAsString(); +// if (!"0".equals(code)) { +// String msg = jsonResult.get("msg").getAsString(); +// throw new BusinessException(code + msg); +// } +// try { +// JsonObject jsonData = jsonResult.getAsJsonObject("data"); +// if (jsonData ==null){ +// jsonData=new JsonObject(); +// } +// return jsonData; +// } catch (Throwable e) { +// BusinessException businessException = new BusinessException(e); +// throw businessException; +// } +// } + + +// private static JsonObject doPost(String url, String json, int timeOut) throws BusinessException { +// Gson gson = new Gson(); +// JsonObject jsonObject = new JsonObject(); +// try { +// LogUtil.debugLog(TAG, "reqest: " + url + " data:" + json.toString()); +// String openApi = HttpClient.post(url, json, "application/json", "OpenApi",timeOut); +// jsonObject = new JsonParser().parse(openApi).getAsJsonObject(); +// } catch (IOException e) { +// BusinessException b = new BusinessException(e); +// if (e instanceof ConnectTimeoutException || e instanceof SocketTimeoutException +// || e instanceof UnknownHostException +// || e instanceof UnknownServiceException || e instanceof SSLException +// || e instanceof SocketException) { +// b.errorDescription = "鎿嶄綔澶辫触锛岃妫�鏌ョ綉缁�"; +// } +// throw b; +// } catch (Throwable e) { +// BusinessException b = new BusinessException(e); +// throw b; +// } +// return jsonObject; +// } + } -- Gitblit v1.8.0