我正在尝试发送一个传递身份验证令牌头的GET请求,以获取一些JSON数据,但是我到处都在获取java.lang.NullPointerExceptions。我已经尝试过JSONObjectRequest,JSONArrayRequest甚至作为StringRequest。
这是代码(为测试添加了JSONObjectRequest和JSONArrayRequest的注释):
public void getJSON() {
final String authToken = getAuth();
String novaURL = getNova();
novaURL = novaURL+"/servers";
/**
___________________________________________________________________
JsonArrayRequest getRequest = new JsonArrayRequest(novaURL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("Nova", response.toString());
setNovaJSON(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Nova", "Error: " + error.getMessage());
}
}
){
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("X-Auth-Token", authToken);
params.put("Accept", "application/json");
params.put("Content-Type", "application/json; charset=utf-8");
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(getRequest);
}
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, novaURL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray servers = response.getJSONArray("servers");
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error to get Instances", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Log.d("Nova", response.toString());
setNovaJSON(response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Nova", "Error: " + error.getMessage());
}
}
) {
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("X-Auth-Token", authToken);
params.put("Accept", "application/json");
params.put("Content-Type", "application/json; charset=utf-8");
return params;
}
};
________________________________________________________________ **/
StringRequest getRequest = new StringRequest(Request.Method.GET,
novaURL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Nova", response.toString());
setNovaJSON(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Nova", "Error: " + error.getMessage());
}
}) {
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("X-Auth-Token", authToken);
params.put("Accept", "application/json");
params.put("Content-Type", "application/json; charset=utf-8");
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(getRequest);
}
异常发生在此行:
RequestQueue queue = Volley.newRequestQueue(this);
11-20 13:44:28.623 14599-14599 / com.x.app D / AndroidRuntime:关闭VM
11-20 13:44:28.623 14599-14599 / com.x.app W / dalvikvm:threadid = 1:线程以未捕获的异常退出(group = 0x41642ce0)
11-20 13:44:28.643 14599-14599 / com.x.app E / AndroidRuntime:致命异常:主
进程:com.stackerz.app,PID:14599
java.lang.NullPointerException
在android.content.ContextWrapper.getCacheDir(ContextWrapper.java:230)
在com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:43)
在com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
很难发现正在发生的事情,因为错误的描述性不是很高。如果它太明显,我会提前道歉,但这是我的第一个应用程序,我无法使其正常运行。我真的很感谢您的帮助。
**更新:
我可能已经找到了有用的东西。我将“ RequestQueue queue = Volley.newRequestQueue(this);”行移到了“与其他变量声明一起位于方法的顶部,并且在调试之后,它似乎在到达JSONObjectRequest或StringRequest命令之前就在那里崩溃了。队列似乎有问题。
** UPDATE2:
我设法按照以下步骤添加了Singleton类扩展应用程序(也必须将其作为应用程序而不是活动添加到清单中)来解决队列问题:https://developer.android.com/training/volley/requestqueue.html
但是我仍然有例外:
java.lang.NullPointerException
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:155)
at org.json.JSONObject.<init>(JSONObject.java:172)
在调试期间,我注意到它从“ getRequest”声明(无论是JSONObjectRequest,JSONArrayRequest还是StringRequest)直接跳转到Response.ErrorListener()。看起来它甚至都没有尝试连接到URL。我测试了从REST浏览器到URL的连接,并确认它可以正常工作,并生成JSON输出。
最佳答案
似乎Volley.newRequestQueue(this)的“ this”为null。
关于android - Volley的JSON GET请求上的java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27031179/