In my Android app, I am sending a POST request on request, and it does not work. Rather, it sends empty parameters.
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 the postman and send a POST request, it will give the desired result.
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();
This is the code I use to send a 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);
And, as I said, the request is executed, but the parameters are not sent along with it.
EDIT
After applying djodjo's answer
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);
After applying my solution, what my codes look like. But I constantly get 409 error.
06-20 19:13:26.405 25586-27084/com.jozuf.blog E/Volley: [6168] BasicNetwork.performRequest: Unexpected response code 409 for https: