简体   繁体   English

使用okhttp的http帖子的UTF编码

[英]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: 我知道以下几种解决方案:

  1. PapaParse at https://github.com/mholt/PapaParse PapaParsehttps://github.com/mholt/PapaParse
    • Pros: It's really lightweight 优点:非常轻巧
    • Cons: Don't know of any, it's always done the job for me 缺点:一无所知,它一直为我完成工作
  2. Or, if you're already using guava or willing to take on the extra app size/method count, it has a thorough selection of parsers/delimiters/URL escapers as described in the javadoc here 或者,如果您已经在使用番石榴或愿意承担额外的应用程序大小/方法数量,则可以按照此处的javadoc中的描述,对语法分析器/定界符/ URL逸出器进行全面选择
    • Pros: It's got everything you could ever need for special char parsing 优点:拥有特殊字符解析所需的一切
    • Cons: guava rarely considered useful enough in its own right to justify the extra method count it brings 缺点:番石榴很少被认为具有足够的实用性来证明其带来的额外方法数量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM