2015-05-14 62 views
0

我發送POST查詢以及來自android設備的字符串數據使用Volley庫。Android Volley:在服務器接收到的空值是用POST請求發送的字符串參數

但是,服務器僅收到參數的null值。我的代碼是這樣的:

 final String param1 = "one"; 
     final String param2 = "124843"; 
     final String param3 = "878942"; 
     final String param4 = "885942"; 

     String url = getIPAddr()+":"+getPort()+"/com.va.jersey.helloworld/hello"; 
     RequestQueue queue = Volley.newRequestQueue(getBaseContext()); 

     StringRequest stringRequest = new StringRequest(Request.Method.POST,url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         showOutput(response); 
        } 
       },new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         showOutput(error.toString()); 
        } 
       }){ 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 
        Map<String, String> params = new HashMap<String, String>(); 
        params.put("param1", param1); 
        params.put("param2", param2); 
        params.put("param3", param3); 
        params.put("param3", param4); 
        return params; 
       }  //attaching POST params 
     }; 
     queue.add(stringRequest); 

我相信這是在StringRequest一些錯誤,歡迎指正......

回答

1

使用如下,讓我知道,如果你正面臨的任何問題

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

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

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

    /** 
    * Passing some request headers 
    * */ 
    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     HashMap<String, String> headers = new HashMap<String, String>(); 
     headers.put("Content-Type", "application/json"); 
     headers.put("apiKey", "xxxxxxxxxxxxxxx"); 
     return headers; 
    } 

}; 

// Adding request to request queue 
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj); 
代碼
+0

錯誤:org.json.JSONException:類型java.lang.String的值nullnullnullnull無法轉換爲JSONObject –

+0

您能否只給我一個您正在調用的url? –

+0

另一個帖子在SO'http:// stackoverflow.com/questions/28135008/unexpected-response-code-500-for-post-method'講述了在ObjectRequest中用'something替換'null'不明白)... –

相關問題