14

我想以以下格式从我的 JAVA 应用程序发送 SMS:
名称:客户名称
ConfNO:1234
日期:2012-05-15
NoOfPax:7
由于我使用外部短信网关,我需要将其作为 URL 从应用程序发布。当我收到短信时,我看不到换行符,而是显示文本 \n。请帮助。我的代码是

String senderid = "SMSALERT";
    String to = "97112345678";
    String text = "Reservation INFO\nName:Customer Name\nConfNO: KN1234\nTime: 12:00PM\n#Pax:5\nThank You for Choosing US";
    String type = "text";
    String ppgHost = "www.yahoo.com";
    String username = "username";
    String password = "password";
    String datetime = "2012-05-15 12:00:00";
    String path = "index.php";
    try {
        HttpClient httpClient = new DefaultHttpClient();
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair("username", username));
        qparams.add(new BasicNameValuePair("password", password));
        qparams.add(new BasicNameValuePair("senderid", senderid));
        qparams.add(new BasicNameValuePair("to", to));
        qparams.add(new BasicNameValuePair("text", text));
        qparams.add(new BasicNameValuePair("type", type));
        qparams.add(new BasicNameValuePair("datetime", datetime));
        URI uri = URIUtils.createURI("http", ppgHost, -1, path, URLEncodedUtils.format(qparams, "UTF-8"), null);
        HttpPost httppost = new HttpPost(uri);
        System.out.println(httppost.getURI());
        HttpResponse response = httpClient.execute(httppost);
        System.out.println(response.getStatusLine());    
4

2 回答 2

33

如果它是一个 URL,您可以将百分比编码值用于新行,%0D%0A尽管%0A(ASCII 10 - 换行) 或%0D(ASCII 13 - 回车) 应该单独工作。

于 2012-05-15T13:27:40.560 回答
0

所有参数都需要进行 URL 编码。此外,用户系统行分隔符而不是硬编码“\n”是一个很好的做法

System.getProperty("line.separator");

于 2012-05-15T13:35:34.973 回答