gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:19:15 24 4
gpt4 key购买 nike

我完成了英文版的申请,目前正在处理阿拉伯语版的申请。尽管我的英语 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com