2

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

Community
  • 1
  • 1
S.Grou
  • 61
  • 1
  • 2
  • 12

3 Answers3

1

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()
        }
S.Grou
  • 61
  • 1
  • 2
  • 12
0

You can simply encode, decode the Jsonobject using URLEncoder, URLDecoder. The example is given below

1.Encode the Jsonobject

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

2.Decode the Jsonobject

    try
    {
      String   decoded = URLDecoder.decode(jsonobject, "UTF-8");
        Log.e("UTF 8",decoded );
    }
    catch (UnsupportedEncodingException e)
    {
        Log.e("utf8", "conversion", e);
    }
sivaprakash
  • 528
  • 6
  • 15
0
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));
  • 1
    Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes – Ran Marciano Mar 25 '21 at 11:12