我无法从SQL数据库中解析阿拉伯语/波斯语文本 . 一切都设置为UTF-8 . 我的SQL数据库文本设置为 utf8_general_ci
. JSON解析器也设置为UTF-8 .
文字显示英文很好 . 但是当我在数据库中使用阿拉伯语/波斯文本时,android会将文本显示为 ???????
.
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
3 回答
也许问题出在服务器端 . 检查从服务器获取的原始字符串,以查看它是否格式正确 .
我认为它可以帮助你将它存储为clob / blob,因为一旦你拥有了服务器端UTF-8的字节,任何客户端代码也可以使用各种String编码格式来显示测试 .
或者我的其他建议,使用webview来显示它,它更成熟地处理这些细微差别 .
我已经研究了一天,最终成功解析了我的阿拉伯json响应从服务器使用以下代码 . 所以,可能对你有所帮助 .
现在,使用jsonText来满足您的进一步要求 .
谢谢