hxb
2022-11-22 b01eb4ec0468ad08f5ae7a92c5e2e7be3096330a
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;
@@ -51,6 +53,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());
@@ -101,58 +104,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 所有字段使用urlParameter拼接,除了appSecret
     * @param json 所有字段使用urlParameter拼接,除了appSecret
     */
    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));