我在Android上的应用程序中的用户名是西里尔语时出错。 我使用TomCat服务器和Eclipse。
对于名称“Алексей”,服务器端向我投掷了这个:
> SEVERE: Servlet.service() for servlet [default] in context with path
> [/AppServer] threw exception java.lang.IllegalArgumentException:
> java.net.URISyntaxException: Illegal character in query at index 37:
> /AppServer/chat?name=???»?µ?????µ??
我的代码:
try {
client = new WebSocketClient(URI.create(WsConfig.URL_WEBSOCKET
+ URLEncoder.encode(name, "UTF-8")), new WebSocketClient.Listener() {
@Override
public void onConnect() {
}
/**
* On receiving the message from web socket server
* */
@Override
public void onMessage(String message) {
Log.d(TAG, String.format("Got string message! %s", message));
parseMessage(message);
}
@Override
public void onMessage(byte[] data) {
Log.d(TAG, String.format("Got binary message! %s",
bytesToHex(data)));
// Message will be in JSON format
parseMessage(bytesToHex(data));
}
/**
* Called when the connection is terminated
* */
@Override
public void onDisconnect(int code, String reason) {
String message = String.format(Locale.US,
"Disconnected! Code: %d Reason: %s", code, reason);
showToast(message);
// clear the session id from shared preferences
utils.storeSessionId(null);
}
@Override
public void onError(Exception error) {
Log.e(TAG, "Error! : " + error);
showToast("Error! : " + error);
}
}, null);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
client.connect();
}
由于URLEncoder.encode(name, "UTF-8")
对我不起,同样“???»?μ?????μ??” = \
有任何建议如何解决?(
答案 0 :(得分:2)
您的代码似乎在这里工作,
String name = "Алексей";
try {
String str = URLEncoder.encode(name, "UTF-8");
System.out.println(str);
System.out.println(URLDecoder.decode(str, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
输出
%D0%90%D0%BB%D0%B5%D0%BA%D1%81%D0%B5%D0%B9
Алексей
确保远程端正在解码UTF-8
答案 1 :(得分:0)
在之前的脚本中对其进行编码,并将已编码的字符串解析为此。 Tomcat问题。