android - add headers google volley request? -


well, new in forum, please if me in this. searched not find how add headers volley request. have code , want add accept-encoding:gzip , api key. appreciate help. here 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); 

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(); } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -