發現網上很多這個問題,但就是沒什么正確的解決方案,決定和阿里的技術支持聊聊天
跟客服聊了一陣,印象最深就是下面這話,心理十萬個草泥馬奔騰而過。

但也只是瞬間經過而已,依賴javase的那肯定能在android上運行。只是他封裝那個jar包不支持而已。
(大概瀏覽了一下jar包代碼,真不懂寫那么復雜是為啥~)
解決思路就是找回阿里巴巴旗下的APIjava版代碼例子,然后去模擬請求
public class AlidayuMessage {
private static final String SIGN_METHOD_MD5 = "md5";
private static final String SIGN_METHOD_HMAC = "hmac";
private static final String CHARSET_UTF8 = "utf-8";
private static final String CONTENT_ENCODING_GZIP = "gzip";
private static final String serverUrl = "http://gw.api.taobao.com/router/rest";
private static final String appKey = "xxxxx"; //你的appkey
private static final String appSecret = "xxxxx"; //你的appSecret
private static final String sessionKey = "";
//業務參數
private static String extend = "";
private static String sms_type = "normal";
private static String sms_free_sign_name = "阿里大魚";
private static String sms_param = "";
private static String rec_num = "";
private static String sms_template_code = "";
/**
* 短信接收號碼。支持單個或多個手機號碼,傳入號碼為11位手機號碼,不能加0或+86。
* 群發短信需傳入多個號碼,以英文逗號分隔,一次調用最多傳入200個號碼。示例:18600000000,13911111111,13322222222
* 是否必須:必須
* @param rec_num
*/
public static void setRecNum(String rec_num) {
AlidayuMessage.rec_num = rec_num;
}
/**
* 短信模板ID,傳入的模板必須是在阿里大魚“管理中心-短信模板管理”中的可用模板。示例:SMS_585014
* 是否必須:必須
* @param sms_template_code
*/
public static void setSmsTemplateCode(String sms_template_code) {
AlidayuMessage.sms_template_code = sms_template_code;
}
/**
* 短信模板變量,傳參規則{"key":"value"},key的名字須和申請模板中的變量名一致,多個變量之間以逗號隔開。
* 示例:針對模板“驗證碼${code},您正在進行${product}身份驗證,打死不要告訴別人哦!”,
* 傳參時需傳入{"code":"1234","product":"alidayu"}
* 是否必須:可選
* @param sms_param
*/
public static void setSmsParam(String sms_param) {
AlidayuMessage.sms_param = sms_param;
}
/**
* 短信簽名,傳入的短信簽名必須是在阿里大魚“管理中心-短信簽名管理”中的可用簽名。如“阿里大魚”已在短信簽名管理中通過審核,
* 則可傳入”阿里大魚“(傳參時去掉引號)作為短信簽名。短信效果示例:【阿里大魚】歡迎使用阿里大魚服務。
* 是否必須:必須
* @param sms_free_sign_name
*/
public static void setSmsFreeSignName(String sms_free_sign_name) {
AlidayuMessage.sms_free_sign_name = sms_free_sign_name;
}
/**
* 公共回傳參數,在“消息返回”中會透傳回該參數;舉例:用戶可以傳入自己下級的會員ID,在消息返回時,
* 該會員ID會包含在內,用戶可以根據該會員ID識別是哪位會員使用了你的應用
* 是否必須:可選
* @param extend
*/
public static void setExtend(String extend) {
AlidayuMessage.extend = extend;
}
public static String SendMsg() throws IOException {
Map<String, String> params = new HashMap<String, String>();
// 公共參數
params.put("method", "alibaba.aliqin.fc.sms.num.send");
params.put("app_key", appKey);
params.put("session", sessionKey);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
params.put("timestamp", df.format(new Date()));
params.put("format", "json");
params.put("v", "2.0");
params.put("sign_method", "hmac");
// 業務參數
params.put("extend", extend);
params.put("sms_type", sms_type);
params.put("sms_free_sign_name", sms_free_sign_name);
params.put("sms_param", sms_param);
params.put("rec_num", rec_num);
params.put("sms_template_code", sms_template_code);
// 簽名參數
params.put("sign", signTopRequest(params, appSecret, SIGN_METHOD_HMAC));
// 請用API
return callApi(new URL(serverUrl), params);
}
private static String callApi(URL url, Map<String, String> params) throws IOException {
String query = buildQuery(params, CHARSET_UTF8);
byte[] content = {};
if (query != null) {
content = query.getBytes(CHARSET_UTF8);
}
HttpURLConnection conn = null;
OutputStream out = null;
String rsp = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Host", url.getHost());
conn.setRequestProperty("Accept", "text/xml,text/javascript");
conn.setRequestProperty("User-Agent", "top-sdk-java");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + CHARSET_UTF8);
out = conn.getOutputStream();
out.write(content);
rsp = getResponseAsString(conn);
} finally {
if (out != null) {
out.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rsp;
}
private static String buildQuery(Map<String, String> params, String charset) throws IOException {
if (params == null || params.isEmpty()) {
return null;
}
StringBuilder query = new StringBuilder();
Set<Map.Entry<String, String>> entries = params.entrySet();
boolean hasParam = false;
for (Map.Entry<String, String> entry : entries) {
String name = entry.getKey();
String value = entry.getValue();
// 忽略參數名或參數值為空的參數
if (isNotEmpty(name) && isNotEmpty(value)) {
if (hasParam) {
query.append("&");
} else {
hasParam = true;
}
query.append(name).append("=").append(URLEncoder.encode(value, charset));
}
}
return query.toString();
}
private static String getResponseAsString(HttpURLConnection conn) throws IOException {
String charset = getResponseCharset(conn.getContentType());
if (conn.getResponseCode() < 400) {
String contentEncoding = conn.getContentEncoding();
if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding)) {
return getStreamAsString(new GZIPInputStream(conn.getInputStream()), charset);
} else {
return getStreamAsString(conn.getInputStream(), charset);
}
} else {// Client Error 4xx and Server Error 5xx
throw new IOException(conn.getResponseCode() + " " + conn.getResponseMessage());
}
}
private static String getResponseCharset(String ctype) {
String charset = CHARSET_UTF8;
if (isNotEmpty(ctype)) {
String[] params = ctype.split(";");
for (String param : params) {
param = param.trim();
if (param.startsWith("charset")) {
String[] pair = param.split("=", 2);
if (pair.length == 2) {
if (isNotEmpty(pair[1])) {
charset = pair[1].trim();
}
}
break;
}
}
}
return charset;
}
private static String getStreamAsString(InputStream stream, String charset) throws IOException {
try {
Reader reader = new InputStreamReader(stream, charset);
StringBuilder response = new StringBuilder();
final char[] buff = new char[1024];
int read = 0;
while ((read = reader.read(buff)) > 0) {
response.append(buff, 0, read);
}
return response.toString();
} finally {
if (stream != null) {
stream.close();
}
}
}
/**
* 對TOP請求進行簽名。
*/
private static String signTopRequest(Map<String, String> params, String secret, String signMethod) throws IOException {
// 第一步:檢查參數是否已經排序
String[] keys = params.keySet().toArray(new String[0]);
Arrays.sort(keys);
// 第二步:把所有參數名和參數值串在一起
StringBuilder query = new StringBuilder();
if (SIGN_METHOD_MD5.equals(signMethod)) {
query.append(secret);
}
for (String key : keys) {
String value = params.get(key);
if (isNotEmpty(key) && isNotEmpty(value)) {
query.append(key).append(value);
}
}
// 第三步:使用MD5/HMAC加密
byte[] bytes;
if (SIGN_METHOD_HMAC.equals(signMethod)) {
bytes = encryptHMAC(query.toString(), secret);
} else {
query.append(secret);
bytes = encryptMD5(query.toString());
}
// 第四步:把二進制轉化為大寫的十六進制
return byte2hex(bytes);
}
/**
* 把字節流轉換為十六進制表示方式。
*/
private static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}
/**
* 對字節流進行HMAC_MD5摘要。
*/
private static byte[] encryptHMAC(String data, String secret) throws IOException {
byte[] bytes = null;
try {
SecretKey secretKey = new SecretKeySpec(secret.getBytes(CHARSET_UTF8), "HmacMD5");
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
bytes = mac.doFinal(data.getBytes(CHARSET_UTF8));
} catch (GeneralSecurityException gse) {
throw new IOException(gse.toString());
}
return bytes;
}
/**
* 對字符串采用UTF-8編碼后,用MD5進行摘要。
*/
private static byte[] encryptMD5(String data) throws IOException {
return encryptMD5(data.getBytes(CHARSET_UTF8));
}
/**
* 對字節流進行MD5摘要。
*/
private static byte[] encryptMD5(byte[] data) throws IOException {
byte[] bytes = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
bytes = md.digest(data);
} catch (GeneralSecurityException gse) {
throw new IOException(gse.toString());
}
return bytes;
}
private static boolean isNotEmpty(String value) {
int strLen;
if (value == null || (strLen = value.length()) == 0) {
return false;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(value.charAt(i)) == false)) {
return true;
}
}
return false;
}
}
以上親測可用。其中我只修改了SendMsg和CallAPI,其他那些函數都是官網上有現成的,別以為全是我自己寫的。。。That‘s all 希望能幫到大家。
