【发布时间】:2017-09-22 17:33:42
【问题描述】:
我需要在 android 应用程序中以 UTF-8 字符编码显示文本,这是我的 JSONAsyncTask 代码:
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Wait...");
dialog.setTitle("Loading");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("news");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
News news = new News();
news.setTitle(object.getString("title"));
news.setDescription(object.getString("description"));
news.setDate(object.getString("date"));
news.setImage(object.getString("image"));
newsList.add(news);
}
return true;
}
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
当然,在我提问之前,先研究一下,看看这些结果:
还有更多……但这些答案并不能解决我的问题。
【问题讨论】:
-
为什么这些结果可以帮助您解决问题?你能澄清一下问题吗?
-
我的问题仍然存在,字符未编码为 utf-8。
-
@FrédéricLetellier 请看截图。
标签: android json encoding utf-8