Skip to content Skip to sidebar Skip to footer

Volley Is Sending Empty Params

In my android app, I am sending a volley POST request and it's not working. Rather it is sending empty parameters. If I enter the url (https://blogurl.com/wp-json/wp/v2/comments?po

Solution 1:

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.

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

publicvoidsubmitComment() {
        Stringcomment=null;
        try {
            comment = URLEncoder.encode(commentContent.getText().toString().trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Stringname=null;
        try {
            name = URLEncoder.encode(commentName.getText().toString().trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Stringemail=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");
            finalStringpostComment="https://blogurl.com/wp-json/wp/v2/comments?post="+comtUrl+"&content="+comment+"&author_name="+name+"&author_email="+email;

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

            JsonObjectRequestpostDetails=newJsonObjectRequest(Method.POST, postComment, null,
                    newResponse.Listener<JSONObject>() {
                        @OverridepublicvoidonResponse(JSONObject response) {
                            Log.d(TAG, response.toString());
                            parseResponse(response);
                            postingComment.dismiss();
                        }
                    },
                    newResponse.ErrorListener() {
                        @OverridepublicvoidonErrorResponse(VolleyError error) {
                            Log.d(TAG, "onErrorResponse for getPost called");
                            VolleyLog.d(TAG, "Error: " + error.getMessage());
                            postingComment.dismiss();
                            if (sthWrongAlert != null) {
                                sthWrongAlert.show();
                            }
                        }
                    });

            intretrytimes=10;
            RetryPolicypolicy=newDefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, retrytimes, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            postDetails.setRetryPolicy(policy);

            //Creating requestqueueRequestQueuerequestQueue= 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(newDefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Solution 2:

getParams() are not called for JsonRequests.

What you can do is just append to

postComment the extra params. for example:

finalStringcomment= URLEncoder.encode(commentContent.getText().toString().trim(), "UTF-8");
        finalStringname= URLEncoder.encode(commentName.getText().toString().trim(), "UTF-8");
        finalStringemail= URLEncoder.encode(commentEmail.getText().toString().trim(), "UTF-8");

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

Solution 3:

You can send the parameters as JSONObject like this:

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

and in JsonObjectRequest you pass it as the third parameter.. you will not need getBodyContentType and getParams anymore

Post a Comment for "Volley Is Sending Empty Params"