How to Set Character Encoding to UTF-8 for JSONObject in the Android

26,606

Updated in 2021

JAVA

Finally i'm solved my problem.

Final code:

 @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();

change status to:

         if (status == HttpStatus.SC_OK) {
             
             HttpEntity entity = response.getEntity();
            

edit this code to:

             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;
 }

Kotlin

 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()
        }
Share:
26,606
S.Grou
Author by

S.Grou

I'm Developing Web &amp; Android App.

Updated on May 09, 2021

Comments

  • S.Grou
    S.Grou almost 3 years

    i need to show text in a UTF-8 Character Encoding in the android App, Here is my Code for JSONAsyncTask in:

    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;
        }
    

    Of course, Before i'm Asking, research and see these result:

    and many more... But that answers can't solve my problem.

    Screenshot