MHopstad
MHopstad

Reputation: 333

Android Volley - Get response Header AND read html

I can't find any solution to this. As of now it seems I have to choose between getting the normal html response or getting only the response headers.

Is there a way to modify my code to get both of these?

Current code (Gives response headers only)

StringRequest stringRequest = new StringRequest(Request.Method.GET, loginURL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                            String responseSession = response.substring(0,response.indexOf(";"));



                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG ).show();
                    }
                }){

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse networkResponse) {
                String sessionId = networkResponse.headers.get("Set-Cookie");
                com.android.volley.Response<String> result = com.android.volley.Response.success(sessionId,
                        HttpHeaderParser.parseCacheHeaders(networkResponse));
                return result;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

Upvotes: 4

Views: 3076

Answers (1)

Krish
Krish

Reputation: 3885

Use a custom request class for this purpose,

public class CustomStringRequest extends Request<CustomStringRequest.ResponseM> {


    private Response.Listener<CustomStringRequest.ResponseM> mListener;

    public CustomStringRequest(int method, String url, Response.Listener<CustomStringRequest.ResponseM> responseListener, Response.ErrorListener listener) {
        super(method, url, listener);
        this.mListener = responseListener;
    }


    @Override
    protected void deliverResponse(ResponseM response) {
        this.mListener.onResponse(response);
    }

    @Override
    protected Response<ResponseM> parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }

        ResponseM responseM = new ResponseM();
        responseM.headers = response.headers;
        responseM.response = parsed;

        return Response.success(responseM, HttpHeaderParser.parseCacheHeaders(response));
    }


    public static class ResponseM {
        Map<String, String> headers;
        String response;
    }

}

And change the code like this ,

CustomStringRequest stringRequest = new CustomStringRequest(Request.Method.GET, loginURL,
                new Response.Listener<CustomStringRequest.ResponseM>() {
                    @Override
                    public void onResponse(CustomStringRequest.ResponseM result) {

                        //From here you will get headers
                        String sessionId = result.headers.get("Set-Cookie");
                        String responseString = result.response;

                    }


                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG ).show();
                    }
                }) {


        };

Upvotes: 5

Related Questions