2016-09-27 23 views
0

處理阿拉伯語我需要阿拉伯值的字符串傳遞給HttpURL作爲參數,但我之前說的,當我打印信息的只顯示問號如何用Java

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參數發送消息,不發送原始消息爲阿拉伯語,發送問號。

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

} 
+3

的可能的複製[打印阿拉伯語Ø r在System.out其他字符集](http://stackoverflow.com/questions/16644247/print-arabic-or-other-charset-in-system-out) –

+0

@YoungMillie問題中提到我不只需要打印阿拉伯語。在轉換爲「utf-8」之後打印阿拉伯文只是一個測試 –

+0

System.out.println會在Windows命令窗口中將您的字符顯示爲'?',但這並不意味着它們實際上是問號字符。這只是意味着命令窗口不能顯示實際的字符。 – VGR

回答

1

假設SendSms.getMessage()返回String,你是怎麼打算與這條線?

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

編碼和解碼信息是一種浪費充其量—如果一切正常,你會剛剛回來你開始的字符串。只有默認編碼爲UTF-8時,它纔會起作用。在這種情況下,它會破壞消息。

當您使用getBytes()String進行編碼時,將使用平臺默認編碼。除非系統的本地字符集支持阿拉伯語,否則任何阿拉伯字符都將替換爲受支持的字符,通常爲?

試試這個:

String message = object.getMessage(); 
0

最起碼,你需要按照埃裏克森的回答意見。如果仍有問題,請繼續閱讀...

Windows命令窗口在可顯示的字符方面相當有限。我不知道各種語言包和代碼頁如何影響它,但我知道英文版的Windows將顯示阿拉伯字符(並且大多數字符在U + 00FF之上)爲?

因此,您的角色完全可能是正確的,他們只是在打印時顯示爲?

而不是使用System.out.println,請使用Logger。然後你可以add a Handler到您的記錄儀(或根Logger),這writes to a 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); 

(順便說一句,MalformedURLException的和ProtocolException類是IOException的兩個子類,所以你只需要throws IOException在。您的方法簽名它本質上涵蓋了其他兩個例外)

在其他一些類,想必您main方法:

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