如何使用Volley发送帖子请求?

时间:2015-02-23 09:28:13

标签: android android-volley android-webservice

String url =" url.php&#34 ;;

    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);
}

这段代码有什么问题?当我运行这段代码&#34;空指针异常&#34;,&#34; jsonObjReq&#34;可能是null。我该如何解决这个问题?

这是我的日志错误

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/

1 个答案:

答案 0 :(得分:0)

您可以初始化排球队列。

试试这个:

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);

}