How to handle Arabic in java

2019-03-03 23:39发布

问题:

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
}

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

}

回答1:

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

String message = new String(object.getMessage().getBytes(), "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.

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 ?.

Try this instead:

String message = object.getMessage();


回答2:

At the very least, you need to follow the advice in erickson’s answer. If there is still a problem, read on…

A Windows command window is pretty limited in what characters it can display. I don’t know how various language packs and code pages affect it, but I know an English edition of Windows will display Arabic characters (and most characters above U+00FF) as ?.

So it’s entirely possible that your characters are correct, they are just being shown as ? when printed.

Instead of using System.out.println, use a Logger. Then you can add a Handler to your Logger (or the root Logger) which writes to a file, and you can examine that file:

private static final Logger logger =
    Logger.getLogger(SmsSender.class.getName());

// ...

public void sendSms(SendSms object) throws IOException {

    String message = new String(object.getMessage().getBytes(),
        StandardCharsets.UTF_8);
    logger.info(() -> "Message=\"" + message + "\"");

    // ...

    int responseCode = con.getResponseCode();
    logger.info(() -> "Sending 'POST' request to URL : " + url);
    logger.info(() -> "Post parameters : " + urlParameters);
    logger.info(() -> "Response Code : " + responseCode);

(By the way, MalformedURLException and ProtocolException are both subclasses of IOException, so you only need throws IOException in your method signature. It inherently covers the other two exceptions.)

In some other class, presumably in your main method:

int logFileMaxSize = 10 * 1024 * 1024;  // 10 MB
Logger.getLogger("").addHandler(
    new FileHandler("%h/Sms-%g.log", logFileMaxSize, 10));