jscherman
jscherman

Reputation: 6189

add headers google volley request?

well, i am new in this forum, please if you could help me in this. i searched but i could not find how to add headers to a volley request. i have this code and i want to add the accept-encoding:gzip and the api key. i would appreciate your help. here is the code:

type = "cafe";
url = "https://maps.googleapis.com/maps/api/place/search/json?location=" + Global.location + "&radius=500&types=" + type + "&sensor=true&key="+placesKey;


RequestQueue rq = Volley.newRequestQueue(context);

JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null, 
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

List<Review> reviews = new ArrayList<Review>();
reviews = Parsing.ParseReviews(response);
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show();

}
});

rq.add(jsonRequest);

Upvotes: 4

Views: 5171

Answers (1)

Leo
Leo

Reputation: 51

JsonObjectRequest jsObjectRequest = new JsonObjectRequest(
            Request.Method.POST,
            url,
            jsonRequest,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "Respuesta en JSON: " + response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "Error Respuesta en JSON: " + error.toString());
                }
            }
    ){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            //return super.getHeaders();
            Map<String, String> params = new HashMap<>();
            params.put("Content-Encoding", "gzip");
            return params;
        }
    };

    VolleySingleton.getInstance(context).addToRequestQueue(jsObjectRequest);

you can add headers in getHeaders().

Edit: How encode in gzip

    JsonObjectRequest jsObjectRequest = new JsonObjectRequest(
            /*same here*/
    ){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("Content-Encoding", "gzip");
            return params;
        }

        @Override
        public byte[] getBody() {
            try{
                return Encode.gzip(super.getBody());
            }catch(IOException e){
                Log.d(TAG, e.getMessage());
                return super.getBody();
            }
        }
    };

    VolleySingleton.getInstance(context).addToRequestQueue(jsObjectRequest);

Encode GZip

public static byte[] gzip(byte[] bytes) throws IOException{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = null;

    try {
        gzos = new GZIPOutputStream(baos);
        gzos.write(bytes);
    }finally {
        if (gzos != null){
            try{
                gzos.close();
            }catch (IOException ignore) {}
        }
    }

    return baos.toByteArray();
}

Upvotes: 3

Related Questions