发送消息时的印地语解码错误。输出就像à¤μà? हà¤μà|?à|? à¤μà¤?हà¤μà¤?à¤?à¤μà|? हà¤μहà|¬à¤μà¤?ह

时间:2014-12-09 12:12:56

标签: java java-ee

    static String username = "######";
    static String password = "#####";
    static String senderid = "###";
    static String message = "वउह";

    static String mobileNo = "08447475458";
    static String mobileNos = "08447475458,08447475458";

    static String scheduledTime = "20110701 02:27:00";
    static HttpURLConnection connection = null;




    public static void main(String[] args) {
        try {

            URL url = new URL("http://msdgweb.mgov.gov.in/esms/sendsmsrequest");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setFollowRedirects(true);
             connection = sendSingleSMS(username, password, senderid,
             mobileNo, message);

            System.out.println("Resp Code:" + connection.getResponseCode());
            System.out.println("Resp Message:"
                    + connection.getResponseMessage());

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }               
    }
// Method for sending single SMS.
    public static HttpURLConnection sendSingleSMS(String username,
            String password, String senderId,
String mobileNo, String message) {
try {
    byte[] bytes = message.getBytes("UTF-8");
        String smsservicetype = "singlemsg"; // For single message.
            String query = "username=" + URLEncoder.encode(username)
                + "&password=" + URLEncoder.encode(password)
                + "&smsservicetype=" + URLEncoder.encode(smsservicetype)
                + "&content=" + URLDecoder.decode(message,"utf-8") + "&mobileno="
                + URLEncoder.encode(mobileNo) + "&senderid="
                + URLEncoder.encode(senderId);

        connection.setRequestProperty("Content-length", String
            .valueOf(query.length()));
        connection.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded;charset = utf-8");
        connection.setRequestProperty("User-Agent",
            "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");
            DataOutputStream output = new DataOutputStream(connection
                    .getOutputStream());
        int queryLength = query.length();
        output.writeBytes(query);
        System.out.println(query);
        DataInputStream input = new DataInputStream(connection
                    .getInputStream()); 
        for (int c = input.read(); c != -1; c = input.read())
            System.out.print((char) c);
        input.close();  
    } catch (Exception e) {
        System.out.println("Something bad just happened.");
        System.out.println(e);
        e.printStackTrace();
    }

    return connection;

我正在使用此代码从网关发送短信但是如果文本是英文的话这个代码很好但是如果我给了一些印地文文本然后它无法读取它并且用户获得了一些字符。

输出类似于

व� हव�� व�हव ��व� हवहব व�ह

4 个答案:

答案 0 :(得分:1)

DataInputStream正在读取字节,但是您必须使用正确的编码将这些字节正确转换为字符。为此,您可以使用构造函数构造String,如下所示:

String hindiCharSequence = new String(bytes, "UTF-8");

答案 1 :(得分:1)

@ChthonicProject的答案是接受的正确答案,这里有点代码。它使用ByteArrayOutputStream来收集字节。 DataInputStream对于读取二进制int等更有用。更好地使用缓冲。和/或读取一个160字节的byte[]缓冲区。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (BufferedInputStream input = new BufferedInputStream(connection
                .getInputStream())) {
    for (int c = input.read(); c != -1; c = input.read()) {
        baos.write(c);
    }
} // Closes input.
String msg = new String(baos.toByteArray(), StandardCharsets.UTF_8);
System.out.print(msg);

你做了什么,将每个字节都视为char。但是使用UTF-8,几个字节可能构成一个char,即16位。

答案 2 :(得分:0)

可能是SMS编码的问题。默认情况下,它使用GSM 7位字母表。 检查您是否可以指定将在SMS编码中使用哪种编码。

答案 3 :(得分:0)

  • 应用此belore查询字符串及其完成

    System.out.println(message);
                String finalmessage = "";
                String sss = "";
                char ch = 0;
    
            for(int i = 0 ; i < message.length();i++){
    
                 ch = message.charAt(i);
                int j = (int) ch;
            //  System.out.println("iiii::"+j);
    
                sss = "&#"+j+";";
                finalmessage = finalmessage+sss;
            }
            System.out.println("ddd"+finalmessage);
    
            message=finalmessage;
            System.out.println("unicoded message=="+message);