【发布时间】: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 编码了。