1

同じクラスのJSONオブジェクトをサーブレットからアプレットに送信していますが、このクラスのすべての文字列変数に、「ą」、「ę」、「ś」、「ń」、「ł」などの文字がありません。 。ただし、「ó」は正常に表示されます(?)。:「Zaznaczprawid ow operacj porównywaniadwóchzmiennychtypu

解決策 もっと詳しく説明できればいいのですが、ヘンリーが気付いたように、この問題の原因はIDEです。グーグルチケットのfarmer1992のクラスを使って解決しました。エスケープされたUnicode文字を出力します(\ u ...)-アプレットが文字を正しくエンコードできる唯一の方法です。また、Tomcatサーブレットを強制的に正しく動作させるために、NetBeans IDEを時々再起動する必要があります(理由はわかりません:))。

サーブレットコード(ソリューションで更新):

//begin of the servlet code extract
public void sendToApplet(HttpServletResponse response, String path) throws IOException
{
    TestServlet x = new TestServlet();
    x.load(path);

    String json = new Gson().toJson(x);
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json;charset=UTF-8");

    PrintWriter out = response.getWriter();
    //out.print(json);
    //out.flush();
    GhettoAsciiWriter out2 = new GhettoAsciiWriter(out);
    out2.write(json);
    out2.flush();

}
//end of the servlet code extract

アプレットコード:

//begin of the applet code extract
public void retrieveFromServlet(String path) throws MalformedURLException, IOException
{
    String encoder = URLEncoder.encode(path, "UTF-8");
    URL urlServlet = new URL("http://localhost:8080/ProjektServlet?action=" + encoder);
    URLConnection connection = urlServlet.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

    InputStream inputStream = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    String json = br.readLine();
    Test y = new Gson().fromJson(json, Test.class);
    inputStream.close();
}
//end of the applet code extract
4

2 に答える 2

1

これらの文字は\uxxxx形式でエンコードする必要があります

このチケットを見ることができます http://code.google.com/p/google-gson/issues/detail?id=388#c4

于 2013-01-12T15:58:41.153 に答える
0

この行で

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

プラットフォームの文字エンコードが使用されます(UTF-8である場合とそうでない場合があります)。で明示的にエンコーディングを設定してみてください

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
于 2013-01-12T15:52:23.280 に答える