19

I am working with Volley, I want to make request to a server which returns me a JSON in the "vissible layer" (I can see it in the web browser). My problem is that the server also returns my in the headers information that I need to get in my App, but I am not able to get the headers from the request.

I have searched a long time but I havent found anything usefull (Onlye adding data to the request Header, but not getting data from the header´s response)

Anyone knows how to implement that?

Chopi
  • 1,123
  • 1
  • 12
  • 23

2 Answers2

29

To get the headers you need to override parseNetworkResponse() in your request.

for example the JsonObjectRequest:

public class MetaRequest extends JsonObjectRequest {

    public MetaRequest(int method, String url, JSONObject jsonRequest, Response.Listener
            <JSONObject> listener, Response.ErrorListener errorListener) {
        super(method, url, jsonRequest, listener, errorListener);
    }

    public MetaRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject>
            listener, Response.ErrorListener errorListener) {
        super(url, jsonRequest, listener, errorListener);
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
            JSONObject jsonResponse = new JSONObject(jsonString);
            jsonResponse.put("headers", new JSONObject(response.headers));
            return Response.success(jsonResponse,
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
}
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
kalin
  • 3,546
  • 2
  • 25
  • 31
  • Thanks!! It works!! I am doing my work in the headers in the parseNetworkResponse instead of adding the headers to the JSONObject returned. – Chopi Apr 06 '16 at 08:54
8

This is an example to work with JSONArray data and headers.

First create your own custom request type implementation:

public class JsonRequest extends JsonObjectRequest {

    public JsonRequest(int method, String url, JSONObject jsonRequest, Response.Listener
            <JSONObject> listener, Response.ErrorListener errorListener) {
        super(method, url, jsonRequest, listener, errorListener);
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));

            JSONObject jsonResponse = new JSONObject();
            jsonResponse.put("data", new JSONArray(jsonString));
            jsonResponse.put("headers", new JSONObject(response.headers));

            return Response.success(jsonResponse,
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
}

and in your request code:

JsonRequest request = new JsonRequest
        (Request.Method.POST, URL_API, payload, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray data = response.getJSONArray("data");
                    JSONObject headers = response.getJSONObject("headers");
                } catch (JSONException e) {
                    Log.e(LOG_TAG, Log.getStackTraceString(e));
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(LOG_TAG, Log.getStackTraceString(error));
            }
        });

See more information about implementing your own custom request in the Volley documentation Implementing a Custom Request.

jesugmz
  • 2,320
  • 2
  • 18
  • 33
  • what is payload? – Joe Apr 03 '18 at 05:14
  • Payload is your request parameters, it could be a JSON. An example: `JSONObject payload = new JSONObject(); payload.put("email", email); payload.put("password", password);` – jesugmz Apr 04 '18 at 08:30