Why does volley's response string use an encoding different from that in the response headers?

mjibson

When doing a volley request (either StringRequest or JsonObjectRequest), using the OkHttp stack, the response string's encoding is set to ISO-8995-1, which is the default encoding. The response has a header: content-type=text/html; charset=utf-8, which should be detected. Why isn't it?

mjibson

Both of those request types call HttpHeaderParser.parseCharset, which is able to determine the charset from the headers. However, it requires that the header be Content-Type, not content-type: it is case sensitive. (I'm not sure of the behavior if using the default HurlStack, it's possible this is an implementation detail difference with the OkHttp stack.)

Solution 1: copy the original request type, but manually override the charset

Solution 2: copy the original request type, but force the expected header to exist

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class JsonUTF8Request extends JsonRequest<JSONObject> {
    public JsonUTF8Request(int method, String url, JSONObject jsonRequest,
                           Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                errorListener);
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            // solution 1:
            String jsonString = new String(response.data, "UTF-8");
            // solution 2:
            response.headers.put(HTTP.CONTENT_TYPE,
                response.headers.get("content-type"));
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            //
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting headers from a response in volley

From Dev

Why Android Volley Response String is so short

From Dev

Custom response from Volley

From Dev

Volley: How to extract JSONObject from String Response? Is it possible?

From Dev

Parsing JSON response from Volley

From Dev

Why does URLConnection.getInputStream() not include the HTTP response headers?

From Dev

Why not all headers can be found in fetch's response?

From Dev

Jersey client response string encoding

From Dev

Remove Content-Length and Transfer-Encoding Headers from spring servlet HTTP Response

From Dev

Response Encoding

From Dev

how to get cookies from volley response

From Dev

Cannot read response returned from volley

From Dev

Parsing simple json response from Volley

From Dev

Android Volley return value from response

From Dev

Why does @Html.AntiForgeryToken() generate different tokens in same response?

From Dev

Why we need to use BufferedReader instead of String while getting the response from server

From Dev

Why does Laravel's Response::json return blank if called from another function?

From Dev

HTTP request / response with response from a different server

From Dev

Why does $http check to see if response data is string

From Dev

Why does WSGI have to use start_response and return an iterator?

From Dev

Why does WSGI have to use start_response and return an iterator?

From Dev

AJAX response is different from java/hibernate returned string

From Java

Read response headers from API response - Angular 5 + TypeScript

From Dev

Accessing response headers from NodeJS/ExpressJS before a response is sent

From Dev

Why does response to my Java GET request have fewer headers than the same URL for Curl

From Dev

How to parse encoding from WebClient Response?

From Dev

Android Volley: gzip response

From Dev

JSONArray response with Volley for Android

From Dev

Volley Not Parsing 404 response

Related Related

  1. 1

    Getting headers from a response in volley

  2. 2

    Why Android Volley Response String is so short

  3. 3

    Custom response from Volley

  4. 4

    Volley: How to extract JSONObject from String Response? Is it possible?

  5. 5

    Parsing JSON response from Volley

  6. 6

    Why does URLConnection.getInputStream() not include the HTTP response headers?

  7. 7

    Why not all headers can be found in fetch's response?

  8. 8

    Jersey client response string encoding

  9. 9

    Remove Content-Length and Transfer-Encoding Headers from spring servlet HTTP Response

  10. 10

    Response Encoding

  11. 11

    how to get cookies from volley response

  12. 12

    Cannot read response returned from volley

  13. 13

    Parsing simple json response from Volley

  14. 14

    Android Volley return value from response

  15. 15

    Why does @Html.AntiForgeryToken() generate different tokens in same response?

  16. 16

    Why we need to use BufferedReader instead of String while getting the response from server

  17. 17

    Why does Laravel's Response::json return blank if called from another function?

  18. 18

    HTTP request / response with response from a different server

  19. 19

    Why does $http check to see if response data is string

  20. 20

    Why does WSGI have to use start_response and return an iterator?

  21. 21

    Why does WSGI have to use start_response and return an iterator?

  22. 22

    AJAX response is different from java/hibernate returned string

  23. 23

    Read response headers from API response - Angular 5 + TypeScript

  24. 24

    Accessing response headers from NodeJS/ExpressJS before a response is sent

  25. 25

    Why does response to my Java GET request have fewer headers than the same URL for Curl

  26. 26

    How to parse encoding from WebClient Response?

  27. 27

    Android Volley: gzip response

  28. 28

    JSONArray response with Volley for Android

  29. 29

    Volley Not Parsing 404 response

HotTag

Archive