当前位置: 首页 > 知识库问答 >
问题:

使用Java调用HTTP API发送SMS

窦啸
2023-03-14
    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应用程序调用这个类来通过我附带的sms APi发送sms。

我已经将我的调用附加到sender类,以便在我想通过按钮单击发送SMS时调用http类。纽扣纽扣;button=(button)findViewById(r.id.nobtn);Button.SetonClickListener(new View.onClickListener(){@override public void onClick(View View){

            //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();


        }


    });

共有1个答案

赵君植
2023-03-14

是什么促成了你的尝试?一个错误?或者它编译得很好,但什么都不做?请共享任何输出

你把“xxxxxxxxx”放在args里是因为你不想让我们看到真正的地址,还是因为你只是在复制粘贴这个例子?

否则我会建议您看一下这里我正在尝试通过android进行http连接

 类似资料:
  • 问题内容: 我想通过Web应用程序将SMS发送到手机,可以吗?我该怎么做? 问题答案: 您可以使用此免费的Java示例程序使用连接到计算机的GSM调制解调器将PC上的SMS发送到COM端口。您还需要从Sun下载并安装Java comm api。 该程序需要以下Java文件才能运行。 SerialConnection.java(此文件用于从Java程序连接到COM端口) SerialConnecti

  • 我试图从域发送邮件,但得到一些错误。

  • 最好的方法是什么?我读过,但大多数话题都过时了。大多数示例都使用“COM”端口,但我在COM连接(在设备管理器中)中找不到USB调制解调器,而不是在“网卡”中。

  • 问题内容: 按照前面的链接(如何发送键盘输出),Java可以模拟使用Robot类按下的键。但是,如何模拟按键组合?如果我想发送组合“ alt-123”,可以使用机器人吗? 问题答案: 简单的答案是。基本上,你需要用的的周围的其他小号

  • 问题内容: 我在活动A中有一个整数数组: 而且我想将该变量发送到活动B,所以我创建了一个新意图并使用putExtra方法: 在活动BI中获取信息: 但这不是真正发送数组,我只是在arrayB上获得了值“ 0”。我一直在寻找一些例子,但没有发现任何事。 问题答案: 你正在使用数组设置额外内容。然后,你尝试获取单个int。 你的代码应为:

  • 我想用grafana显示指标(只是一个简单的每秒/分钟请求数计数器)。我使用石墨收集指标。 当我使用java向graphite报告请求时: 问题是,一切都毫无例外地运行,但是Graphite服务器没有得到任何指标。知道她怎么了吗? 此外,该指标的名称(mymetrics.requests.successful)没有出现在指标系列的列表中。 非常感谢提前!