【问题标题】:How to Set Character Encoding to UTF-8 for JSONObject in the Android如何在 Android 中为 JSONObject 设置字符编码为 UTF-8
【发布时间】: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;
    }

当然,在我提问之前,先研究一下,看看这些结果:

还有更多……但这些答案并不能解决我的问题。

Screenshot

【问题讨论】:

  • 为什么这些结果可以帮助您解决问题?你能澄清一下问题吗?
  • 我的问题仍然存在,字符未编码为 utf-8。
  • @FrédéricLetellier 请看截图。

标签: android json encoding utf-8


【解决方案1】:

2021 年更新

JAVA

我的问题终于解决了。

最终代码:

 @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 == HttpStatus.SC_OK) {
             
             HttpEntity entity = response.getEntity();
            

将此代码编辑为:

             String data = EntityUtils.toString(response.getEntity(), cz.msebera.android.httpclient.protocol.HTTP.UTF_8);
             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;
 }

科特林

 fun doInBackground(vararg urls: String?): Boolean? {
    try {
        val httppost = HttpGet(urls[0])
        val httpclient: HttpClient = DefaultHttpClient()
        val response: HttpResponse = httpclient.execute(httppost)
        val status: Int = response.getStatusLine().getStatusCode()

           if (status == HttpStatus.SC_OK) {

                val entity: HttpEntity = response.getEntity()
        }

【讨论】:

    【解决方案2】:
    String URL = your_URL;
    URL obj = new URL(URL);
    HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
    
    connection.setRequestMethod("GET");
    
    BufferedReader in = new BufferedReader(new 
    InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票
    【解决方案3】:

    您可以使用 URLEncoder, URLDecoder 简单地对 Jsonobject 进行编码、解码。示例如下

    1.编码Jsonobject

        try
        {
            String encoded = URLEncoder.encode(jsonobject, "UTF-8");
            Log.e("UTF 8",encoded );
        }
        catch (UnsupportedEncodingException e)
        {
            Log.e("utf8", "conversion", e);
        }
    

    2.解码Jsonobject

        try
        {
          String   decoded = URLDecoder.decode(jsonobject, "UTF-8");
            Log.e("UTF 8",decoded );
        }
        catch (UnsupportedEncodingException e)
        {
            Log.e("utf8", "conversion", e);
        }
    

    【讨论】:

    • 它不工作... :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2018-02-17
    • 2013-01-18
    相关资源
    最近更新 更多