Volley JsonObjectRequest中的Nullpointerexception

时间:2014-07-16 21:52:48

标签: java android nullpointerexception android-volley

我很难尝试使用Google排球在Genymotion模拟器中执行简单的JsonObjectRequest。但是下面代码的最后一行(JsonObjectRequest的实例化)会导致Nullpointerexception。

有人可以解释一下我在这里缺少什么吗?

JSONObject requestParams = new JSONObject();
try{
    requestParams.put("username","aaaa");
    requestParams.put("password","bbbb");
}catch(JSONException e){
    e.printStackTrace();
}

Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
@Override
    public void onResponse(JSONObject response) {
        //Do something
    }
};

Response.ErrorListener errorListener = new Response.ErrorListener(){
    @Override
    public void onErrorResponse(VolleyError volleyError) {
    }
};

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, "192.168.178.155/user", requestParams, listener, errorListener);

logcat的

java.lang.NullPointerException
        at com.android.volley.Request.<init>(Request.java:129)
        at com.android.volley.toolbox.JsonRequest.<init>(JsonRequest.java:58)
        at com.android.volley.toolbox.JsonObjectRequest.<init>(JsonObjectRequest.java:48)
        at my.package.name.RequestHandler.performRequest(RequestHandler.java:68)

1 个答案:

答案 0 :(得分:4)

您需要在网址中加入http://

即。做

request = new JsonObjectRequest(Request.Method.GET, "http://192.168.178.155/user", requestParams, listener, errorListener); 

而不是

request = new JsonObjectRequest(Request.Method.GET, "192.168.178.155/user", requestParams, listener, errorListener);