Converting String to Android JSONObject loses utf-8

前端 未结 4 499
盖世英雄少女心
盖世英雄少女心 2021-01-06 02:12

I am trying to get a (JSON formatted) String from a URL and consume it as a Json object. I lose UTF-8 encoding when I convert the String to JSONObject.

This is The f

4条回答
  •  灰色年华
    2021-01-06 02:15

    You can update your code as the following:

        private static String getUrlContents(String theUrl) {
            StringBuilder content = new StringBuilder();
            try {
                URL url = new URL(theUrl);
                URLConnection urlConnection = url.openConnection();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
    
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    content.append(line).append("\n");
                }
                bufferedReader.close();
            } catch(Exception e) {
                e.printStackTrace();
            }
    
            return content.toString().trim();
        }
    

提交回复
热议问题