好吧,我是这个论坛的新手,如果可以的话,请帮助我.我搜索了但我找不到如何向齐射请求添加标头.我有此代码,我想添加accept-enCoding:gzip和API密钥.我将感谢您的帮助.这是代码:
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>() {@OverrIDepublic voID onResponse(JsONObject response) {List<RevIEw> revIEws = new ArrayList<RevIEw>();revIEws = Parsing.ParseRevIEws(response);}}, new Response.ErrorListener() {@OverrIDepublic voID one rrorResponse(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 one rrorResponse(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);
您可以在getheaders()中添加标题.
编辑:如何在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);
编码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();}
总结 以上是内存溢出为你收集整理的android-添加标头谷歌排球请求吗?全部内容,希望文章能够帮你解决android-添加标头谷歌排球请求吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)