0

字符串 url = "url.php";

    ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.show();

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.POST, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d("Response", response.toString());
                    // pDialog.hide();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("Eror", "Error: " + error.getMessage());
                    // pDialog.hide();
                }
            }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            params.put("username", "prp");
            params.put("password", "1234");


            return params;
        }

    };

    // Adding request to request queue
    ApplicationController.getInstance().addToRequestQueue(jsonObjReq);
}

这段代码有什么问题?当我运行此代码“空指针异常”时,“jsonObjReq”可能为空。我该如何解决这个问题?

这是我的日志错误

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.sencide.AndroidLogin.signUp(AndroidLogin.java:96)
at com.sencide.AndroidLogin.onClick(AndroidLogin.java:172)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

我参考此链接以获取帖子数据:- http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/

4

1 回答 1

0

您可以初始化 Volley 队列。

尝试这个 :

class YourClass {
    private RequestQueue mRequestQueue;
    mRequestQueue =  Volley.newRequestQueue(this);

    ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.show();

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.POST, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d("Response", response.toString());
                    // pDialog.hide();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("Eror", "Error: " +         error.getMessage());
                    // pDialog.hide();
                }
            }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            params.put("username", "prp");
            params.put("password", "1234");


            return params;
        }

    };

    mRequestQueue.add(jsonObjReq);

}
于 2015-02-23T12:32:40.457 回答