安卓接入腾讯短信( SMS )服务

不论是阿里还是腾讯给的短信jar包导入安卓项目都会报包冲突,而且忽视相同文件后报找不到xxxx的错误。

所以使用http的方式是比较方便的。

但相对于阿里的http版的demo的复制粘贴,腾讯就给了一句话,请根据API编写代码敷衍了事。

http版  指定模板单发  demo如下

其他按照api稍作修改即可

new Thread(new Runnable() {
                    @Override
                    public void run() {
                   
                        // 短信应用SDK AppID
                        int appid = ; // 1400开头
                        // 短信应用SDK AppKey
                        String appkey = "";
                        // 需要发送短信的手机号码
                        String phoneNumbers = "138xxxxxxx";
                        // 短信模板ID,需要在短信应用中申请
                        // NOTE: 这里的模板ID`7839`只是一个示例,真实的模板ID需要在短信控制台中申请
                        int templateId = ;
                        // 签名
                        // NOTE: 这里的签名"腾讯云"只是一个示例,真实的签名需要在短信控制台中申请,
                        // 另外签名参数使用的是`签名内容`,而不是`签名ID`
                        String smsSign = "";
                        //验证码
                        try {
                            String code = "4567";
                            long random = new Random(System.currentTimeMillis() / 1000L).nextLong() % 900000L + 100000L;
                            long now = System.currentTimeMillis() / 1000L;
                            JSONObject obj = new JSONObject();
                            JSONObject telObj = new JSONObject();

                            telObj.put("nationcode", "86");
                            telObj.put("mobile", phoneNumbers);
                            obj.put("tel", telObj);
                            obj.put("params", new JSONArray().put(String.valueOf(code)));
                            obj.put("sign", smsSign);
                            obj.put("sig", getSig(appkey, random, now, phoneNumbers));
                            obj.put("tpl_id", templateId);
                            obj.put("time", now);
                            obj.put("extend", "");
                            obj.put("ext", "");

                            System.out.println(obj.toString());


                            HttpURLConnection urlConnection = (HttpURLConnection) new URL("https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=" + appid + "&random=" + random).openConnection();
                            urlConnection.setConnectTimeout(3000);
                            urlConnection.setDoOutput(true);
                            urlConnection.setDoInput(true);
                            byte[] mydata = obj.toString().getBytes("utf-8");
                            //设置请求报文头,设定请求数据类型
                            urlConnection.setRequestProperty("Content-Type",
                                    "application/x-www-form-urlencoded");
                            //设置请求数据长度
                            urlConnection.setRequestProperty("Content-Length",
                                    String.valueOf(mydata.length));
                            //设置POST方式请求数据
                            urlConnection.setRequestMethod("POST");
                            OutputStream outputStream = urlConnection.getOutputStream();
                            outputStream.write(mydata);
                            outputStream.flush();
                            int responseCode = urlConnection.getResponseCode();
                            if (responseCode == 200) {
                                InputStream inputStream = urlConnection.getInputStream();
                                byte[] buf = new byte[1024];
                                int len;
                                while ((len = inputStream.read(buf)) != -1) {
                                    System.out.println("接收到消息:" + new String(buf, 0, len, "utf-8"));
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

SHA256:

转载于:点击打开链接

 private String getSig(String appkey, long random, long time, String phoneNumbers) {
        StringBuffer buffer = new StringBuffer("appkey=")
                .append(appkey)
                .append("&random=")
                .append(random)
                .append("&time=")
                .append(time)
                .append("&mobile=");
        buffer.append(phoneNumbers);
        return getSHA256StrJava(buffer.toString());

    }

    /**
     * 利用java原生的摘要实现SHA256加密
     *
     * @param str 加密后的报文
     * @return
     */
    public static String getSHA256StrJava(String str) {
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }

    /**
     * 将byte转为16进制
     *
     * @param bytes
     * @return
     */
    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                //1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }



猜你喜欢

转载自blog.csdn.net/qq_26559913/article/details/80282019