如何在Java中处理阿拉伯语 [英] How to handle Arabic in java

查看:557
本文介绍了如何在Java中处理阿拉伯语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一串阿拉伯数值作为参数传递给HttpURL,但是在我说出打印消息时,它只显示问号

i need to pass a string of arabic value to the HttpURL as a parameter but before i do say when i print the message its showing only question marks

public void sendSms(SendSms object) throws MalformedURLException,ProtocolException,IOException  {

    String message=new String(object.getMessage().getBytes(), "UTF-8");
    System.out.println(message);//printing only question marks
}

即使我将邮件作为url参数发送时,也不会将其原始消息发送为阿拉伯语,而是发送问号.

and even when i send the message as url parameter its not sending the original message as arabic its sending the question marks.

public void sendSms(SendSms object) throws MalformedURLException, ProtocolException,IOException  {

    String message=new String(object.getMessage().getBytes(), "UTF-8");
    System.out.println(message);
    PrintStream out = new PrintStream(System.out, true, "UTF-8");
    out.print(message);
    String charset="UTF-8";


    URL url = new URL("http://62.215.226.164/fccsms_P.aspx");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestProperty("Accept-Charset", charset);

    con.setRequestMethod("POST");

    //con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept-Language", "en-US,en,ar_KW;q=0.5");
    con.setRequestProperty("Content-Type", "text/html;charset=utf-8");
    String urlParameters =       "UID=test&P=test&S=InfoText&G=965"+object.getPhone()+"&M= Hello "+object.getName()+" "+message+" &L=A";

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

}

推荐答案

假设SendSms.getMessage()返回String,那么您打算在此行中做什么?

Assuming that SendSms.getMessage() returns a String, what did you intend with this line?

String message = new String(object.getMessage().getBytes(), "UTF-8");

对消息进行编码和解码充其量是浪费的-如果它起作用,您将只取回开始时使用的字符串.并且仅在默认编码为UTF-8时才有效.在这种情况下,它破坏了邮件.

Encoding and decoding the message is a waste at best—if it works, you will just get back the string you started with. And it will only work if the default encoding is UTF-8. In this case, it's corrupting the message.

getBytes()编码String时,将使用平台默认编码.除非系统的本机字符集支持阿拉伯语,否则任何阿拉伯语字符都将替换为受支持的字符,通常为?.

When you encode a String with getBytes(), the platform default encoding is used. Unless the system's native character set supports Arabic, any Arabic character will be replaced with a supported character, usually ?.

尝试以下方法:

String message = object.getMessage();

这篇关于如何在Java中处理阿拉伯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆