1


私は、Apache minaを使用して、AndroidクライアントとJavaサーバー実装を備えたクライアントサーバーアプリケーションに取り組んでいます。アプリケーションは、ドイツ語の「ウムラウテ」 (「ä」、「ö」、「ü」など) を処理できる必要があります。アプリケーションの両方の部分で、エンコードとデコードに UTF-8 を使用しています。しかし、ウムラウトを含む文字列をクライアントからサーバーに送信すると、サーバー上の文字列に「?」が含まれます。ウムラウトの代わりに。奇妙なことに、サーバーからクライアントに文字列を送信すると、すべての文字が正しい方法で表示されます。

ここに私のサーバーコードがあります:

@Override
public void messageReceived(IoSession session, Object message)
        throws Exception {
    super.messageReceived(session, message);
    String mess = (String) message;
    System.out.println(mess);
    Gson gson = new Gson();
    String command = gson.fromJson(mess, CommandObject.class).getCommand();
    JsonParser jParser = new JsonParser();
    JsonObject jsonObj = (JsonObject) jParser.parse(mess);
    jsonObj = jsonObj.getAsJsonObject("object");
    String json = jsonObj.toString();
    String answer = this.commandMap.get(command).execute(json);
    session.write(answer);
}

public static void main(String[] args) throws IOException {
    IoAcceptor acceptor = new NioSocketAcceptor(1);
    DefaultIoFilterChainBuilder filterChain = acceptor.getFilterChain();
    ProtocolCodecFilter filter = new ProtocolCodecFilter(
            new TextLineCodecFactory(Charset.forName("UTF-8")));
    filterChain.addLast("codec", filter);
    acceptor.setHandler(new IOHandler());
    acceptor.bind(new InetSocketAddress(4630));
}


クライアントからデータを送信するために使用するコードは次のとおりです。

private static final Charset ENC_CHARSET = Charset.forName("UTF-8");


public TCPSocket(final Socket socket) throws IOException {
    this.socket = socket;
    this.inStream = new BufferedReader(new InputStreamReader(
            socket.getInputStream(), TCPSocket.ENC_CHARSET));
    this.outStream = new BufferedWriter(new OutputStreamWriter(
            socket.getOutputStream(), TCPSocket.ENC_CHARSET));
}

public void sendLine(final String line) throws IOException {
    this.outStream.write(line);
    this.outStream.newLine();
    this.outStream.flush();
}

public String receiveLine() throws IOException {
    return this.inStream.readLine();
}


ここで何が問題なのか誰か知っていますか?よろしくお願いします。

4

0 に答える 0