【问题标题】:Volley is sending empty paramsVolley 正在发送空参数
【发布时间】:2016-06-20 01:43:07
【问题描述】:

在我的 android 应用程序中,我正在发送一个凌空 POST 请求,但它不工作。而是发送空参数。

如果我在邮递员中输入 url (https://blogurl.com/wp-json/wp/v2/comments?post=20081&author_name=Ozuf&author_email=myemail@my.com&content=This_is_a_sampe_comment) 并发送 POST 请求,它会产生所需的结果。

Postman 为 JAVA OKHttp 生成的代码如下所示:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url("https://blogurl.com/wp-json/wp/v2/comments?post=20081&author_name=Ozuf&author_email=myemail@my.com&content=This_is_a_sampe_comment")
  .post(null)
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "23f2c2587-e9eb-5446-3d73-a1a1a6814683")
  .build();

Response response = client.newCall(request).execute();

这是我用来发送 POST 请求的代码:

public void submitComment() {
        final String comment = commentContent.getText().toString().trim();
        final String name = commentName.getText().toString().trim();
        final String email = commentEmail.getText().toString().trim();
        Log.d(TAG, "submitComment called");


        if (allFieldsAreValid()) {
            Log.d(TAG, "All fields are valid");
            final String postComment = "https://blogurl.com/wp-json/wp/v2/comments?";

            final PostingComment postingComment = PostingComment.newInstance();
            postingComment.show(getFragmentManager(), "fragmentDialog");

            JsonObjectRequest postDetails = new JsonObjectRequest(Method.POST, postComment, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d(TAG, response.toString());
                            parseResponse(response);
                            postingComment.dismiss();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d(TAG, "onErrorResponse for getPost called");
                            VolleyLog.d(TAG, "Error: " + error.getMessage());
                            postingComment.dismiss();
                            if (sthWrongAlert != null) {
                                sthWrongAlert.show();
                            }
                        }
                    }) {
                @Override
                public String getBodyContentType() {
                    return "application/x-www-form-urlencoded; charset=UTF-8";
                }

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("post", comtUrl);
                    params.put("author_name", name);
                    params.put("author_email", email);
                    params.put("content", comment);
                    return params;
                }

            };

            int retrytimes = 10;
            RetryPolicy policy = new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, retrytimes, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            postDetails.setRetryPolicy(policy);

            //Creating requestqueue
            RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

            //Adding request queue
            requestQueue.add(postDetails);

            Log.d(TAG, "Data sent is " + comment + name + email);


        } else {
            Log.d(TAG, "All fields are not valid");
        }

    }

正如我之前所说,请求为真,但参数并未随之发送。


编辑

应用 djodjo 回答后

public void submitComment() {
        String comment = null;
        try {
            comment = URLEncoder.encode(commentContent.getText().toString().trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String name = null;
        try {
            name = URLEncoder.encode(commentName.getText().toString().trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String email = null;
        try {
            email = URLEncoder.encode(commentEmail.getText().toString().trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "submitComment called");


        if (allFieldsAreValid()) {
            Log.d(TAG, "All fields are valid");
            final String postComment = "https://blogurl.com/wp-json/wp/v2/comments?post="+comtUrl+"&content="+comment+"&author_name="+name+"&author_email="+email;

            final PostingComment postingComment = PostingComment.newInstance();
            postingComment.show(getFragmentManager(), "fragmentDialog");

            JsonObjectRequest postDetails = new JsonObjectRequest(Method.POST, postComment, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d(TAG, response.toString());
                            parseResponse(response);
                            postingComment.dismiss();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d(TAG, "onErrorResponse for getPost called");
                            VolleyLog.d(TAG, "Error: " + error.getMessage());
                            postingComment.dismiss();
                            if (sthWrongAlert != null) {
                                sthWrongAlert.show();
                            }
                        }
                    }) {
                @Override
                public String getBodyContentType() {
                    return "application/x-www-form-urlencoded; charset=UTF-8";
                }

            };

            int retrytimes = 10;
            RetryPolicy policy = new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, retrytimes, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            postDetails.setRetryPolicy(policy);

            //Creating requestqueue
            RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

            //Adding request queue
            requestQueue.add(postDetails);

            Log.d(TAG, "Data sent is " + comment + name + email);


        } else {
            Log.d(TAG, "All fields are not valid");
        }

    }

应用他的解决方案后,这就是我的代码的样子。但我不断收到错误 409。

06-20 19:13:26.405 25586-27084/com.jozuf.blog E/Volley: [6168] BasicNetwork.performRequest: Unexpected response code 409 for https://blog.url/wp-json/wp/v2/comments?post=20081content=Yyggggbbhhgggggg&author_nameRrrauthor_emailgf%40ff.com

【问题讨论】:

  • 这种类型的请求不会像你期望的那样发送参数。你期待它的 json 响应吗?
  • 消息是什么
  • @djodjo 是的,我期待它的 json 响应。我的问题最后一行的消息。

标签: android wordpress android-volley postman


【解决方案1】:

在聊天中调试后,发现问题是POST请求,但参数仍在请求url中发送。

在您的情况下,发送POST 响应将使参数作为需要修复的多部分形式进入正文。用下面的代码更新你的代码

public void submitComment() {
        String comment = null;
        try {
            comment = URLEncoder.encode(commentContent.getText().toString().trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String name = null;
        try {
            name = URLEncoder.encode(commentName.getText().toString().trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String email = null;
        try {
            email = URLEncoder.encode(commentEmail.getText().toString().trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "submitComment called");


        if (allFieldsAreValid()) {
            Log.d(TAG, "All fields are valid");
            final String postComment = "https://blogurl.com/wp-json/wp/v2/comments?post="+comtUrl+"&content="+comment+"&author_name="+name+"&author_email="+email;

            final PostingComment postingComment = PostingComment.newInstance();
            postingComment.show(getFragmentManager(), "fragmentDialog");

            JsonObjectRequest postDetails = new JsonObjectRequest(Method.POST, postComment, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d(TAG, response.toString());
                            parseResponse(response);
                            postingComment.dismiss();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d(TAG, "onErrorResponse for getPost called");
                            VolleyLog.d(TAG, "Error: " + error.getMessage());
                            postingComment.dismiss();
                            if (sthWrongAlert != null) {
                                sthWrongAlert.show();
                            }
                        }
                    });

            int retrytimes = 10;
            RetryPolicy policy = new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, retrytimes, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            postDetails.setRetryPolicy(policy);

            //Creating requestqueue
            RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

            //Adding request queue
            requestQueue.add(postDetails);

            Log.d(TAG, "Data sent is " + comment + name + email);


        } else {
            Log.d(TAG, "All fields are not valid");
        }

    }

thread 中解释了重复问题

只需更改请求以具有此重试策略,它就会起作用

request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

【讨论】:

  • 谢谢,从来不知道请求被提出了两次。这解决了它。
  • 很高兴知道。如果有效,请接受答案。
  • 我已经做到了。太感谢了。以为我用过request.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
【解决方案2】:

getParams() 不会为 JsonRequests 调用。

你可以做的只是追加到

postComment 额外的参数。例如:

final String comment = URLEncoder.encode(commentContent.getText().toString().trim(), "UTF-8");
        final String name = URLEncoder.encode(commentName.getText().toString().trim(), "UTF-8");
        final String email = URLEncoder.encode(commentEmail.getText().toString().trim(), "UTF-8");

final String postComment = "https://blogurl.com/wp-json/wp/v2/comments?comment="+comment+"&name="+name+"&email="+email;

【讨论】:

  • 然后我使用postComment作为url发送请求,丢弃参数,对吧
  • 我已经应用了您的答案,并根据我遇到的问题更新了我的问题。
  • 这是一个与 wp-api 相关的不同问题,有几个错误和 409 的可能性
  • 但是如果我将相同的网址复制并粘贴到邮递员中,它可以无缝运行。
【解决方案3】:

您可以像这样将参数作为 JSONObject 发送:

JSONObject params = new JSONObject("{\"post\":\"" + comtUrl + "\"," + 
...
+ "}"

在 JsonObjectRequest 中,您将其作为第三个参数传递.. 您将不再需要 getBodyContentTypegetParams

【讨论】:

  • 这是完全不同的要求。你不能只是更改请求并希望服务器会处理它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 2019-12-04
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多