我正在尝试从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 onErrorResponse(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
请求。请帮忙..
答案 0 :(得分:18)
嗯,事情很简单,非常精确。将标头传递给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 onErrorResponse(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;
}
};
答案 1 :(得分:0)
您只需使用下面的 var lowerlimit = DateTime.Now.AddMonths(-6);
var upperlimit = DateTime.Now.AddMonths(6);
var rank =db.rankings.Where(a=>a.Date>lowerlimit && a.Date<upperlimit).ToList();
方法
Request.Method.GET
其中参数是
public void methodName(String url,String perameter){
url = url +"?"+ perameters;
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url,
new JSONObject(params), new Response.Listener<JSONObject>() {
/// your other code
}
答案 2 :(得分:-1)
我遇到了同样的问题,但我通过仔细检查URL解决了这个问题,确保它是正确的,如:http://192.168.123.102:8080/xxx/xxx