UTF-8 encoding in Volley Requests UTF-8 encoding in Volley Requests android android

UTF-8 encoding in Volley Requests


If you know that absolutely all of the files you are requesting will be in the UTF-8 format, which it sounds like you do, then you might consider forcing your Volley request to return UTF-8 formatted strings. You could accomplish this by subclassing the standard JSON request. Something like this:

public class Utf8JsonRequest extends JsonRequest<JSONObject> {    ...    @Override    protected Response<JSONObject> parseNetworkResponse (NetworkResponse response) {        try {            String utf8String = new String(response.data, "UTF-8");            return Response.success(new JSONObject(utf8String), HttpHeaderParser.parseCacheHeaders(response));        } catch (UnsupportedEncodingException e) {            // log error            return Response.error(new ParseError(e));        } catch (JSONException e) {            // log error            return Response.error(new ParseError(e));        }    }}


I have same problem like this and i solve it using UTF-8 charset.

String str = "";try {     str = new String(strFromService.getBytes("ISO-8859-1"), "UTF-8");} catch (UnsupportedEncodingException e) { e.printStackTrace();}String decodedStr = Html.fromHtml(str).toString();

I hope this will work for you


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