凌空抽空参数 [英] Volley is sending empty params

查看:93
本文介绍了凌空抽空参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中,我正在发送截击POST请求,但该请求不起作用。而是发送空参数。

In my android app, I am sending a volley POST request and it's not working. Rather it is sending empty parameters.

如果我输入网址( 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请求,即可产生所需的结果。

If I enter the 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) in postman and send a POST request it yields the desired result.

和代码由Postman生成的对于Java OKHttp来说是这样的:

And the code generated by Postman looks like this for 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请求的代码:

This is the code I am using to send the POST request:

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

    }

就像我之前说的那样,请求变为true,但参数不随其发送。

And Like I said before, the request goes true but the parameters are not sent along with it.

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。

After applying his solution, that's how my codes looks like. But I am constantly getting error 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


推荐答案

在聊天室中调试之后,发现的问题是它是 POST 请求,但参数仍在请求URL中发送。

After the debugging in chat, the issue found was that it is a POST request but the parameters are still being sent in the request url.

在您的情况下,发送 POST 响应将使参数以多部分形式进入体内,需要固定。使用下面的代码更新您的代码

In your case sending a POST response will make the param to go in the body as a multipart form which needs to be fixed. Update you code with below

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

    }

重复的问题在此得到解释线程

The duplicate issue is explained in this thread

只需更改请求以具有此重试策略即可,

Just change the request to have this retry policy and it will work

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

这篇关于凌空抽空参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆