From b3513b1713bb979d0a69c5a8c4ddcd038f184e6e Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期二, 22 十一月 2022 13:59:20 +0800 Subject: [PATCH] 可以测试 --- AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpClient.java | 80 ++++++++++++++++++++------------------- 1 files changed, 41 insertions(+), 39 deletions(-) diff --git a/AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpClient.java b/AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpClient.java index c0e53e0..0cb4d26 100644 --- a/AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpClient.java +++ b/AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpClient.java @@ -7,6 +7,7 @@ import com.google.gson.GsonBuilder; 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.mobilecommon.utils.LogUtil; import java.io.ByteArrayOutputStream; @@ -51,6 +52,7 @@ conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); conn.setRequestProperty("Content-Type", contentType); + conn.setRequestProperty("Authorization","Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJqdGkiOiI5MWFjM2FlOWVlMDY0MDY4OWU2NmQzMjQ3MzkxZmQ3MiIsImNvbXBhbnlJZCI6IjIwMiIsInJvbGUiOiIiLCJoZWFkZXJQcmVmaXgiOiJCZWFyZXIgIiwidXNlckFjY291bnQiOiJ3eHIiLCJ0ZW5hbnRJZCI6IjIwIiwidXNlclR5cGUiOiJVU0VSX0IiLCJ0b2tlblR5cGUiOiJhY2Nlc3NfdG9rZW4iLCJ1c2VyTmFtZSI6Ind4ciIsIm9wZW5BcHBsaWNhdGlvbklkIjoiMCIsInVzZXJJZCI6IjEzOTIwMzU1NjgyMDQ0MjMxNjkiLCJleHAiOjE2NjkxMDE2NTEsIm5iZiI6MTY2OTA5NDQ1MX0.BhY5cZDds87lpCOVscCjiH-EnvFSuOFjgXvVkWvGr4elUHFwFTNumhtvfCcQY4Nbu0MNTaPbb36ydNPtXHLAZrXVPdijk_WvnhoTBvA_RUOZOrQ1G4Iep3ZjgHuHam1bsCmgATQqDJR66q3ZiLwe5o_Snce2rAK4aI2Das9uN_M"); conn.connect(); outputStream = conn.getOutputStream(); outputStream.write(paramStr.getBytes()); @@ -101,58 +103,58 @@ /** - * 娣诲姞sign瀛楁 - */ - public static JsonObject encrypt(Map<String, Object> map) { - final String timestamp = String.valueOf(System.currentTimeMillis()); - final String appKey = "AppKey"; - final String appSecret = "AppSecret"; - - - JsonObject json = new JsonObject(); - for (Map.Entry<String, Object> entry : map.entrySet()) { - if (entry.getValue() instanceof String) { - json.addProperty(entry.getKey(), (String) entry.getValue()); - } else if (entry.getValue() instanceof Boolean) { - json.addProperty(entry.getKey(), (Boolean) entry.getValue()); - } else if (entry.getValue() instanceof Number) { - json.addProperty(entry.getKey(), (Number) entry.getValue()); - } else if (entry.getValue() instanceof Character) { - json.addProperty(entry.getKey(), (Character) entry.getValue()); - } else if (entry.getValue() instanceof JsonElement) { - json.add(entry.getKey(), (JsonElement) entry); - } - } - - - if (json != null) { - json.addProperty("appKey", appKey); - json.addProperty("timestamp", timestamp); - json.addProperty("sign", getSign(map, appSecret)); - } - return json; - } - - /** * 闇�瑕佹寜瀛楁瘝鎺掑簭 * - * @param map 鎵�鏈夊瓧娈典娇鐢╱rlParameter鎷兼帴锛岄櫎浜哸ppSecret + * @param json 鎵�鏈夊瓧娈典娇鐢╱rlParameter鎷兼帴锛岄櫎浜哸ppSecret */ - private static String getSign(Map<String, Object> map, String appSecret) { - String builder = jsonToUrlParameter(map) + + public static String getSign(JsonObject json, String appSecret) { + String builder = jsonToUrlParameter(json) + appSecret; return HDLMD5Utils.encodeMD5(builder); } - private static String jsonToUrlParameter(Map<String, Object> map ) { + private static JsonObject getBodyJson(Map<String,Object> map) { + + JsonObject object = new JsonObject(); + + for (Map.Entry<String, Object> entry : map.entrySet()) { + object.addProperty(entry.getKey(), entry.getValue().toString()); + } + return object; + } + + + + + /** + * 娣诲姞sign瀛楁 + */ + public static JsonObject encrypt(Map<String,Object> map ) { + final String timestamp = String.valueOf(System.currentTimeMillis()); + final String appKey ="HDL-HOME-APP-TEST"; + final String appSecret ="WeJ8TY88vbakCcnvH8G1tDUqzLWY8yss"; + final JsonObject json = getBodyJson(map); + if (json != null) { + json.addProperty("appKey", appKey); + json.addProperty("timestamp", timestamp); + json.addProperty("sign", getSign(json, appSecret)); + } + return json; + } + + + + private static String jsonToUrlParameter(JsonObject object) { + final Map<String, String> map = new Gson().fromJson(object, new TypeToken<Map<String, String>>() { + }.getType()); final Uri.Builder builder = new Uri.Builder(); List<String> list = new ArrayList<>(map.keySet()); Collections.sort(list); for (String key : list) { //鍒ゆ柇褰撳墠鍊兼槸鍚﹂渶瑕佸弬涓庣鍚嶏紝淇濇寔璺熶簯绔竴鑷� - if (IfValueNeedSign(map.get(key).toString())) { - builder.appendQueryParameter(key, map.get(key).toString()); + if (IfValueNeedSign(map.get(key))) { + builder.appendQueryParameter(key, map.get(key)); // HDLSDKLog.e("瑕佺鍚嶏細" + key + " 锛�" + map.get(key)); } else { // HDLSDKLog.e("涓嶉渶瑕佺鍚嶏細" + key + " 锛�" + map.get(key)); -- Gitblit v1.8.0