2015-02-23 105 views
0

String 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/

+0

'當我運行這段代碼「空指針異常」'請出示日誌 – 2015-02-23 09:47:41

+0

後完全錯誤的logcat。 – Sanket990 2015-02-23 09:53:58

+0

當我調試指針去這行「ApplicationController.getInstance()。addToRequestQueue(jsonObjReq);」。 – 2015-02-23 11:02:27

回答

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

}