Android volley post methode参数缺失

时间:2016-06-21 17:05:45

标签: android android-volley

我正在使用android volley库将数据发布到后端服务。但我无法通过我的请求发送任何参数。我已经完成了所有提到的here。但没有一个适合我。我使用的post方法是:

 public static void post()
{

    // Tag used to cancel the request
    String tag_json_obj = "json_obj_req";

    String url = "http://myUrl";


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

                @Override
                public void onResponse(JSONObject response) {
                    Log.d("TAG", response.toString());

                }
            }, new Response.ErrorListener() {

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

        }
    }) {

        /**
         * Passing some request headers
         * */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();

            return headers;
        }

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded";
        }



        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("key", "value");


            return params;
        }
    };
// Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);


}

响应总是&#34;参数丢失&#34;。 我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:2)

如果您正在使用JSONObjectRequest,则可以尝试此操作。

    String url = "http://myurl";
    Map<String, String> params = new HashMap<String, String>();
    params.put("key", value);

    RequestQueue queue = Volley.newRequestQueue(getActivity());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
            url, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        success = jsonObject.getInt("success");
                        message = jsonObject.getString("message");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Activity activity = getActivity();
                    if (volleyError instanceof NoConnectionError) {
                        String errormsg = "Check your internet connection";
                        Toast.makeText(activity, errormsg, Toast.LENGTH_LONG).show();
                    }
                }
            });
    queue.add(jsonObjectRequest);

代码更可能与您的代码相同。检查我要发布数据的行。我非常肯定这会奏效!

答案 1 :(得分:1)

问题是您正在接近请求,就像您正在制作stringRequest一样。您引用的链接专门讨论如何制作stringRequest

jsonObjectRequest实际上允许你将json对象放入构造函数本身,而不是像这样使用覆盖方法getParams()

    String url = "some_url";
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put(Constants.LOGIN_EMAIL_ID, email);
        jsonObject.put(Constants.LOGIN_PASSWORD, password);
    }catch(JSONException e){
        Log.d("JSON error", e.getMessage(), e);
    }

    JsonObjectRequest jsObjRequest =  new JsonObjectRequest(url, jsonObject, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("RESPONSE", response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if(error.getMessage()!=null){
                Log.d("RESPONSE", error.getMessage());
            }
        }
    });


    VolleySingleton.getInstance(activity).getRequestQueue().add(jsObjRequest);