Android:如何在 JSONStringer 中为 Web 服务请求设置编码?

标签 android .net json web-services encoding

我完成了英文版的申请,目前正在处理阿拉伯语版的申请。尽管我的英语 Web 服务运行良好,但我的阿拉伯语 Web 服务似乎有问题,我觉得在使用 JSONStringer 类构造 JSON 请求时需要指定编码类型 (utf-8)。有办法吗?

这是构造我的 JSON 请求的方法示例,

public static String initLoginJSONRequest(String username, String password){

String parentString = null;
String childString = null;

try{

    childString = new JSONStringer()
    .object()
        .key("username").value(username)
        .key("password").value(password)
    .endObject()
    .toString();

    parentString = new JSONStringer()
    .object()
        .key("UserCredentials").value(childString)
    .endObject()
    .toString();

}catch(JSONException e){
    e.printStackTrace();
}


return parentString;

编辑

我还想补充一点,我在 HttpPost 中指定我的编码是 utf-8,如下所示,

HttpPost post = new HttpPost(getUrl);
             StringEntity se = new StringEntity(jsonString);
             se.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
             se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));

             post.setEntity(se);
             response = client.execute(post);

但它没有在 Web 服务端(用 .NET 编写)正确接收我的阿拉伯字符。

最佳答案

尝试通过ISO-8859-1改变UTF-8

此处的答案可以帮助您: Android - Read an XML file with HTTP GET

关于Android:如何在 JSONStringer 中为 Web 服务请求设置编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11492770/

相关文章:

c# - servicestack GlobalRequestFilters 请求 Dto 为 null

java - 如何从url(json数据)显示另一个 Activity 中列表中单个项目的详细信息?

java - ObjectMapper.writeValueAsString 输出中的重复值

javascript - 为什么 jquery 对这个选择器很生气?

java - 在哪里设置RecycleView setAdapter以快速加载图像?

android - 我在哪里可以了解如何使网站适应多种 Android 分辨率?

.net - app.config 中元素的顺序重要吗?

c# - 连续检查 Queue<T>

AlertDialog 上的 Android RatingBar

php - 值在 TextView 中未传递到 Mysql 数据库