[英]UTF encoding for http post using okhttp
I'm trying to post some data to a web server using OkHttp 我正在尝试使用OkHttp将一些数据发布到Web服务器
public static void sendNewPost(NewPost post, Context context) throws IOException, NoSuchFieldException{
RequestBody body = post.buildBody();
URL url = buildURL("newreply.php");
openPage(url, body, context);
}
public class NewPost {
...
public setMessage(String m) {
MESSAGE = "Probando con la letra: ñ, áéíóú";
}
public RequestBody buildBody() throws NoSuchFieldException{
...
RequestBody postVariables = new FormBody.Builder()
.add(MESSAGE_KEY, MESSAGE)
.build();
return postVariables;
}
}
If I send that, the server will show something like this: 如果我发送该消息,服务器将显示以下内容:
Probando con la letra: ñ, áéÃóú
However If I change the setMessage function: 但是,如果我更改setMessage函数:
public setMessage(String m) {
m = "Probando con la letra: ñ, áéíóú";
MESSAGE = "";
for (int i = 0; i < m.length(); i++) {
switch (m.charAt(i)) {
case 'ñ':
MESSAGE += "%u00F1";
break;
case 'á':
MESSAGE += "%u00E1";
break;
case 'é':
MESSAGE += "%u00E9";
break;
case 'í':
MESSAGE += "%u00ED";
break;
case 'ó':
MESSAGE += "%u00F3";
break;
case 'ú':
MESSAGE += "%u00FA";
break;
default:
MESSAGE += m.charAt(i);
break;
}
}
}
Things works as expected and the server shows the right thing. 事情按预期工作,服务器显示正确的事情。 I reckon it is replacing the spanish characters for their UTF-16 codes, but I wonder if there is a better way to encode the full string, or make OkHttp handle it.
我认为它将用西班牙语字符替换其UTF-16代码,但是我想知道是否有更好的方法来编码完整字符串或使OkHttp处理它。
Edit: When I log the network traffic I see that, if my message is: "ññ" the browser encodes it as: 编辑:当我记录网络流量时,我看到的是,如果我的消息是:“ññ”,浏览器会将其编码为:
...&message=%F1%F1&...
while when I do it on android & okhttp, the message is sent as: 而当我在android&okhttp上执行此操作时,该消息将发送为:
...&message=%C3%B1%C3%B1&...
Thats the thing I want, how to make OkHttp encode the string like the browser does. 那就是我想要的,如何使OkHttp像浏览器一样对字符串进行编码。
Here's a couple of solutions that I know of: 我知道以下几种解决方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.