Android Volley:在服务器上接收使用POST请求发送的String参数的空值

时间:2015-05-14 11:21:06

标签: android http-post android-volley

我使用POST库从Android设备发送Volley查询以及字符串数据。

然而,服务器仅接收参数的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 个答案:

答案 0 :(得分: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);