【问题标题】:Calling HTTP API Using Java to send SMS使用Java调用HTTP API发送短信
【发布时间】:2016-03-03 11:31:46
【问题描述】:

我已经附加了我的 HTTP 类,现在我需要在单击按钮时发送我的 SMS 时调用这个类。我似乎没有找到任何解决方案。任何帮助将不胜感激。

    public class Sender {
    // Username that is to be used for submission
    String username;
    // password that is to be used along with username
    String password;
    // Message content that is to be transmitted
    String message;
    /**
     * What type of the message that is to be sent
     * <ul>
     * <li>0:means plain text</li>
     * <li>1:means flash</li>
     * <li>2:means Unicode (Message content should be in Hex)</li>
     * <li>6:means Unicode Flash (Message content should be in Hex)</li>
     * </ul>
     */
    String type;
    /**
     * Require DLR or not
     * <ul>
     * <li>0:means DLR is not Required</li>
     * <li>1:means DLR is Required</li>
     * </ul>
     */
    String dlr;
    /**
     * Destinations to which message is to be sent For submitting more than one
     * destination at once destinations should be comma separated Like
     * 91999000123,91999000124
     */
    String destination;
    // Sender Id to be used for submitting the message
    String source;
    // To what server you need to connect to for submission
    String server;
    // Port that is to be used like 8080 or 8000
    int port;
    public Sender(String server, int port, String username, String password,
                  String message, String dlr, String type, String destination,
                  String source) {
        this.username = username;
        this.password = password;
        this.message = message;
        this.dlr = dlr;
        this.type = type;
        this.destination = destination;
        this.source = source;
        this.server = server;
        this.port = port;
    }
    private void submitMessage() {
        try {
             // Url that will be called to submit the message
            URL sendUrl = new URL("http://" + this.server + ":" + this.port
                    + "/bulksms/bulksms");
            HttpURLConnection httpConnection = (HttpURLConnection) sendUrl
                    .openConnection();
              // This method sets the method type to POST so that
                // will be send as a POST request
            httpConnection.setRequestMethod("POST");
            // This method is set as true wince we intend to send
             // input to the server
            httpConnection.setDoInput(true);
        // This method implies that we intend to receive data from server                                                         httpConnection.setDoOutput(true);
// Implies do not use cached data
            httpConnection.setUseCaches(false);
         // Data that will be sent over the stream to the server.
            DataOutputStream dataStreamToServer = new DataOutputStream(
                    httpConnection.getOutputStream());
            dataStreamToServer.writeBytes("username="
                    + URLEncoder.encode(this.username, "UTF-8") +    "&password="
                    + URLEncoder.encode(this.password, "UTF-8") + "&type="
                    + URLEncoder.encode(this.type, "UTF-8") + "&dlr="
                    + URLEncoder.encode(this.dlr, "UTF-8") + "&destination="
                    + URLEncoder.encode(this.destination, "UTF-8") + "&source="

                    + URLEncoder.encode(this.source, "UTF-8") + "&message="
                    + URLEncoder.encode(this.message, "UTF-8"));
            dataStreamToServer.flush();
            dataStreamToServer.close();
           // Here take the output value of the server.
            BufferedReader dataStreamFromUrl = new BufferedReader(
                    new InputStreamReader(httpConnection.getInputStream()));
            String dataFromUrl = "", dataBuffer = "";
            // Writing information from the stream to the buffer
            while ((dataBuffer = dataStreamFromUrl.readLine()) != null) {
                dataFromUrl += dataBuffer;
            }
   /**
     * Now dataFromUrl variable contains the Response received from the
     * server so we can parse the response and process it accordingly.
    */
            dataStreamFromUrl.close();
            System.out.println("Response: " + dataFromUrl);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public void main(String[] args) {
        try {
 // Below exmaple is for sending Plain text
            Sender s = new   Sender("http:", 8080, "xxxxxxx",
                    "xxxxx", "Congratulations! You just gave someone a priceless gift - LIFE! Thank you for donating." +
                    "Your next donation date is 13/6/16. Get ", "1", "0", "xxxxxxx",
                    "xxxx");
            s.submitMessage();
       // Below exmaple is for sending unicode
            Sender s1 = new Sender("smpp2.routesms.com", 8080, "xxxx",
                    "xxx", convertToUnicode("test for unicode").toString(),
                    "1", "2", "919869533416", "Update");
            s1.submitMessage();
        } catch (Exception ex) {
        }
    }
    /**
     * Below method converts the unicode to hex value
     * @param regText
     * @return
     */
    private StringBuffer convertToUnicode(String regText) {
        char[] chars = regText.toCharArray();
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            String iniHexString = Integer.toHexString((int) chars[i]);
            if (iniHexString.length() == 1) {
                iniHexString = "000" + iniHexString;
            }
            else if (iniHexString.length() == 2)
                iniHexString = "00" + iniHexString;
            else if (iniHexString.length() == 3)
                iniHexString = "0" + iniHexString;
            hexString.append(iniHexString);
        }
        System.out.println(hexString);
        return hexString;
    }
}

我似乎无法从 Android 应用调用此类以通过我附加的短信 API 发送短信。

当我想在单击按钮时发送 SMS 时,我已将呼叫附加到 sender 类以呼叫 http 类。 按钮按钮; button = (Button) findViewById(R.id.noBtn); button.setOnClickListener(new View.OnClickListener() { @覆盖 public void onClick(查看视图) {

            //createUserAppointment();

            Sender sender = new Sender("xxxxxxxxxxxxx", 8080, "xxxxx",
                    "xxxx", "Congratulations! You just gave someone a priceless gift - LIFE! Thank you for donating." +
                    "Your next donation date is 13/6/16.", "1", "0", "xxxxxxxxx",
                    "Moja");
            sender.submitMessage();


        }


    });

【问题讨论】:

    标签: java android rest soap http-headers


    【解决方案1】:

    是什么产生了你的尝试?一个错误?或者它编译得很好但什么也没做?也请分享任何输出

    您在 args 中输入“xxxxxxxxx”是因为您不希望我们看到真实地址,还是因为您只是复制粘贴示例?

    否则我建议你看看这里 I am trying http connection over android

    【讨论】:

    • 我设法用您的解决方案解决了这个问题,但这次我的用户名和密码值无效。相同的用户名和密码是我用来登录发送我的群发短信。我对这个有想法。
    • 我也设法解决了这个问题。我在通话中使用了错误的服务器名称。从 API 提供者那里获得了正确的服务器名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2017-02-23
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    相关资源
    最近更新 更多