wxr
2022-11-18 15b3ee439476e88df66991afb4e0a4d7a8e73422
AndroidOpenDemo/DeviceAddModule/src/main/java/com/mm/android/deviceaddmodule/openapi/HttpClient.java
@@ -1,11 +1,24 @@
package com.mm.android.deviceaddmodule.openapi;
import android.net.Uri;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class HttpClient {
@@ -85,4 +98,87 @@
        }
        return resultData;
    }
    /**
     * 添加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
     */
    private static String getSign(Map<String, Object> map, String appSecret) {
        String builder = jsonToUrlParameter(map) +
                appSecret;
        return HDLMD5Utils.encodeMD5(builder);
    }
    private static String jsonToUrlParameter(Map<String, Object> map ) {
        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());
//                HDLSDKLog.e("要签名:" + key + " :" + map.get(key));
            } else {
//                HDLSDKLog.e("不需要签名:" + key + " :" + map.get(key));
            }
        }
        return builder.build().getQuery();
    }
    /**
     * 判断当前值是否需要参与签名,保持跟云端一致
     * 空字符串不参与
     * 数组,集合,对象不参与
     *
     * @param valueStr
     * @return
     */
    private static boolean IfValueNeedSign(String valueStr) {
        if (TextUtils.isEmpty(valueStr))
            return false;
        final char[] strChar = valueStr.substring(0, 1).toCharArray();
        final char firstChar = strChar[0];
        //System.out.println("getJSONType firstChar = "+firstChar);
        if (firstChar != '{' && firstChar != '[')
            return true;
        return false;
    }
}