Maria Schrein
Maria Schrein

Reputation: 31

Response with non-Latin letters using Volley (UTF-8)

I'm using Volley to download text files from a website.

This is the content of a sample text file:

NEON Wönn 30€ Kostüm größter Spaß TESTTESTTESTTEST★★★★TESTTEST:::TEST

I put that in Notepad and selected 'Encoding UTF-8' in the SaveFileDialog. In Filezilla in the server manager I selected 'Force UTF-8' before I uploaded the file.

When I download it with Volley the response will look like this:

NEON Wönn 30⬠Kostüm gröÃter Spaà TESTTESTTESTTESTââââTESTTEST:::TEST

Here is my method:

    public static void getRequest(String url) {
    RequestQueue queue = Volley.newRequestQueue(activity);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //response is gibberish :/
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("VolleyError", error.toString());

        }
    });
     stringRequest.setShouldCache(false);
    // Add the request to the RequestQueue.
    queue.add(stringRequest);
}

Is there a way to fix that by forcing Volley to use UTF-8 Encoding?

Upvotes: 0

Views: 1060

Answers (2)

Steve
Steve

Reputation: 126

I'm working with a server (that I do not control) that sends back UTF-8 responses without setting charset in the content-type header. Volley currently defaults to ISO-8859-1 in this case.

I simply wanted to change the default charset without forcing every response to UTF-8. I ended up just using parseNetworkResponse() to intercept Volley's response processing and check to see if charset is missing from the response headers. If it is missing, I force it to look like the server said "charset=UTF-8" and then just let the normal processing continue.

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {

    // Volley's default charset is "ISO-8859-1". If no charset is specified, we want to default to UTF-8. 
    String charset = HttpHeaderParser.parseCharset(response.headers, null);

    if (null == charset) {
        String contentType = response.headers.get("Content-Type");
        contentType = (null != contentType) ? (contentType + ";charset=UTF-8") : "charset=UTF-8";
        response.headers.put("Content-Type", contentType);
    }

    return super.parseNetworkResponse(response);
}

Upvotes: 0

Maria Schrein
Maria Schrein

Reputation: 31

I had to Override this method:

@Override
protected Response<String> parseNetworkResponse(
        NetworkResponse response) {

    String strUTF8 = null;
    try {
        strUTF8 = new String(response.data, "UTF-8");

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();
    }
    return Response.success(strUTF8,
            HttpHeaderParser.parseCacheHeaders(response));
}

Upvotes: 2

Related Questions