我正在使用 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();
}
有人知道这里有什么问题吗?在此先感谢您的帮助!