【问题标题】:how to send utf-8 encoded strings with volley post request android如何使用 volley post request android 发送 utf-8 编码字符串
【发布时间】:2016-04-05 09:59:52
【问题描述】:

我正在使用 volley 库在我的 android 应用程序中发送网络请求。 我使用 POST 方法发送 StringRequest 并使用

发送 post 参数

getParams() 方法。

现在我想用 UTF-8 编码发送这些 post 参数中的一些字符串。我已经尝试为此添加以下内容。

@Override
public String getBodyContentType() {
   return "application/x-www-form-urlencoded; charset=UTF-8";
}

但它不能正常工作。 我也尝试使用 getHeaders 函数添加标题,但它也不起作用。

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> header = new HashMap<String, String>();
    header.put("Content-Type", "application/json; charset=utf-8");
    return header;
}

我该怎么做,如果有人对此有任何想法,请帮助我。

【问题讨论】:

  • But it is not working fine。你是什​​么意思 ?请准确。 but it is also not working. 什么是“不工作”?我想知道你为什么要对 utf-8 做任何事情。这不是默认行为吗?你遇到了哪些问题?在您的帖子中告诉所有内容。不在 cmets 中。
  • 向我们展示您的getParams() 方法
  • 我通过将以下内容添加到字符串中来管理它。 byte[] bytes = "hello".getBytes("UTF-8"); String body = new String(bytes, Charset.forName("UTF-8")); 现在我可以用这个字符串发送 UTF-8 编码了。

标签: android encoding utf-8


【解决方案1】:

尝试将您的文本转换为UTF-8 并发布

byte[] bytes = "text".getBytes("UTF-8"); 
String sendBody = new String(bytes, Charset.forName("UTF-8"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 2011-06-25
    • 1970-01-01
    • 2014-06-09
    相关资源
    最近更新 更多