From 409c13d0886117d5ee4ff42186d90b19a692b804 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期二, 21 二月 2023 14:06:36 +0800
Subject: [PATCH] 增加品牌认证

---
 AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpClient.java |   95 ++++++++++++++++++++++++++---------------------
 1 files changed, 53 insertions(+), 42 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..3dffb94 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,7 +7,9 @@
 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.LCDeviceEngine;
 import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -23,6 +25,13 @@
 
 public class HttpClient {
     private static final String TAG=HttpClient.class.getSimpleName();
+
+    private static boolean showErrorInfo = false;
+
+    public static void SetShowErrorInfo(boolean isShow){
+        showErrorInfo = isShow;
+    }
+
     /**
      * post
      *
@@ -33,7 +42,7 @@
      * @return
      */
     public static String post(String urlStr, String paramStr, String contentType, String debugTag,int timeOut) throws IOException {
-        LogUtil.debugLog(TAG,urlStr+"..."+paramStr);
+        LogUtil.debugLog(TAG,urlStr+"..debuglogHdlkaede.."+paramStr);
         HttpsURLConnection conn = null;
         String resultData = "";
         OutputStream outputStream = null;
@@ -51,6 +60,7 @@
             conn.setUseCaches(false);
             conn.setInstanceFollowRedirects(true);
             conn.setRequestProperty("Content-Type", contentType);
+            conn.setRequestProperty("Authorization","Bearer "+ LCDeviceEngine.newInstance().accessToken);
             conn.connect();
             outputStream = conn.getOutputStream();
             outputStream.write(paramStr.getBytes());
@@ -73,8 +83,11 @@
                 baos.flush();
                 if (debugTag != null)
                     LogUtil.debugLog(debugTag, "request data " + resultData);
-            } else
-                throw new IOException(String.format("http_get failed: status=%d", responseCode));
+            } else {
+                if(showErrorInfo) {
+                    throw new IOException(String.format("http_get failed: status=%d : url=%d", responseCode,urlStr));
+                }
+            }
         } catch (IOException e) {
             throw e;
         } catch (Throwable e) {
@@ -101,58 +114,56 @@
 
 
     /**
-     * 娣诲姞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 JsonObject json = getBodyJson(map);
+        if (json != null) {
+            json.addProperty("appKey", CONST.APPID);
+            json.addProperty("timestamp", timestamp);
+            json.addProperty("sign", getSign(json, CONST.SECRET));
+        }
+        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