我试图从GET请求发送一些身份验证标头,我尝试使用Volley JsonObjectRequest调用:
Map<String,String> params=new HashMap<String,String>(); params.put("token","fghjbvjhnjjk"); activity.showDialog(); JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url, new JsONObject(params), new Response.Listener<JsONObject>() { @OverrIDe public voID onResponse(JsONObject response) { Log.d(tag, response.toString()); activity.hIDeDialog(); try { activity.onRequestServed(response, code); } catch (JsONException e) { e.printstacktrace(); } } }, new Response.ErrorListener() { @OverrIDe public voID one rrorResponse(VolleyError error) { VolleyLog.d(tag, "Error: " + error.getMessage()); Log.e(tag, "Site Info Error: " + error.getMessage()); Toast.makeText(activity.getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); activity.hIDeDialog(); try { activity.onRequestServed(null,code); } catch (JsONException e) { e.printstacktrace(); } } }); req.setShouldCache(true);
但它显示:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference at com.androID.volley.Request.<init>(Request.java:136) at com.androID.volley.toolBox.JsonRequest.<init>(JsonRequest.java:58) at com.androID.volley.toolBox.JsonObjectRequest.<init>(JsonObjectRequest.java:47)
我在某处读到你可以通过制作一个hashmap来传递头文件,从而用该参数创建一个新的JsonObject.也许这将适用于POST请求.请帮忙..
解决方法:
嗯,事情很简单,非常精确.将标头传递给GET或POST请求,您需要覆盖JsonObjectRequest类中的getheaders方法.这是如何做到的:
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url, null, new Response.Listener<JsONObject>() { @OverrIDe public voID onResponse(JsONObject response) { Log.d(tag, response.toString()); activity.hIDeDialog(); try { activity.onRequestServed(response, code); } catch (JsONException e) { e.printstacktrace(); } } }, new Response.ErrorListener() { @OverrIDe public voID one rrorResponse(VolleyError error) { VolleyLog.d(tag, "Error: " + error.getMessage()); Log.e(tag, "Site Info Error: " + error.getMessage()); Toast.makeText(activity.getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); activity.hIDeDialog(); try { activity.onRequestServed(null,code); } catch (JsONException e) { e.printstacktrace(); } } }) { /** * Passing some request headers */ @OverrIDe public Map<String, String> getheaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); //headers.put("Content-Type", "application/Json"); headers.put("key", "Value"); return headers; } };
总结 以上是内存溢出为你收集整理的android – Volley JsonObjectRequest在GET请求中发送头文件全部内容,希望文章能够帮你解决android – Volley JsonObjectRequest在GET请求中发送头文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)