【问题标题】:Android Volley: null value being received at server for String params sent with POST requestAndroid Volley:在服务器上接收到与 POST 请求一起发送的字符串参数的空值
【发布时间】:2015-05-14 11:21:06
【问题描述】:

我正在使用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有错误,请更正...

【问题讨论】:

    标签: android http-post android-volley


    【解决方案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);
    

    【讨论】:

    • 错误:org.json.JSONException:java.lang.String 类型的值 nullnullnullnull 无法转换为 JSONObject
    • 你能给我你打电话的网址吗?
    • 另一篇关于 SO http://stackoverflow.com/questions/28135008/unexpected-response-code-500-for-post-method 的帖子讲述了将 ObjectRequest 中的 null 替换为某些东西...(我不明白)...
    • url 是127.0.0.1:8080/com.va.jersey/helloworld/hello....我从HttpRequester firefox 插件得到了正确的响应
    • 是 POST 还是 GET?另外你能告诉我json或字符串输出吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 2017-05-04
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多