UTF-8 encoding in Volley Requests

后端 未结 8 1509
天命终不由人
天命终不由人 2020-12-06 02:17

In my Android app I am loading json data with a Volley JsonArrayRequest. The data were created by myself and I saved them with Sublime with UTF-8 encoding. When

8条回答
  •  一个人的身影
    2020-12-06 02:34

    donot use try{} catch in the onResponse block, that is giving some problem in my code , rather than that you can implement like this .

    @Override 
    onResponse(String s) {
    
    s= fixEncoding(s);
    Toast.makeToast(this,s,Toast.LENGTH_LONG).show();
    
    }
    

    and i think you will get the required result

     public static String fixEncoding(String response) {
                try {
                    byte[] u = response.toString().getBytes(
                            "ISO-8859-1");
                    response = new String(u, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    return null;
                }
                return response;
            }
    

提交回复
热议问题