UTF-8解码不起作用

时间:2014-06-18 21:36:52

标签: java android utf-8 mina


我正在使用Apache mina开发一个带有Android客户端和Java服务器实现的客户端 - 服务器应用程序。该应用程序必须能够处理德语“Umlaute”(例如'ä','ö','ü')。在应用程序的两个部分中,我使用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();
}


有人知道这里的问题是什么吗?在此先感谢您的帮助!

0 个答案:

没有答案