我在我的应用程序中使用 JSON 类。但是,我对阿拉伯字母表有疑问。每当我尝试写入我的数据库时,我都会得到 ??????对于阿拉伯语。对于英语,这是没有问题的。我在我的 php 中设置了 setchar 以与 utf-8 兼容,并且在数据库中,该字段是 utf-general-ci。但是,我认为问题出在我的 JSON 类上。这是我的 JSON 类。
public class JSONParser {
public JSONObject makeHttpRequest(String url, List<NameValuePair> pairs)
{
try
{
HttpParams httpParams = new BasicHttpParams();
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);
int timeoutSocket = 10000;
HttpConnectionParams.setConnectionTimeout(httpParams, timeoutSocket);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(url);
if (pairs != null)
{
httpPost.setEntity(new UrlEncodedFormEntity(pairs) );
}
HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null)
{
String ret = EntityUtils.toString(httpEntity);
return new JSONObject(ret.substring(ret.indexOf("{"), ret.lastIndexOf("}") + 1));
}
}
catch (Exception ex)
{
}
return null;
}
}
我相信它使用的是默认加密。如何将其修复为 UTF-8。好吧,与我使用 http.Post.setEntity 一致,我尝试将“UTF-8”作为方法 setEntity 中的另一个参数。它给我一个错误。所以,你有什么建议吗????提前致谢
最佳答案
尝试替换
String ret = EntityUtils.toString(httpEntity);
用这条线
String ret = EntityUtils.toString(entity, HTTP.UTF_8);
也许它不是 json 而是 HttpClient
关于php - JSON 类无法处理阿拉伯语单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31906148/