Android Volley:服务器接收到空值,用于使用 POST 请求发送的字符串参数

Android Volley: null value being received at server for String params sent with POST request

我正在使用 Volley 库从 android 设备发送 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 中存在一些错误,请更正...

使用下面给出的代码,如果您遇到任何问题,请告诉我

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