0

Good afternoon everyone I did a volley connection to my localserver. It turns out, the connection works fine but my parameters are not getting accepted in my MysqlPHP script. I believe the parameters are not getting sent correctly. Here is the code

try {
 RequestQueue jr = Volley.newRequestQueue(this);
 HashMap<String, String> params = new HashMap<String, String>();
 params.put("username", username);
 params.put("password", password);
 Log.d("The paramet ready", "Ready to go");
 JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params),
 new Response.Listener<JSONObject>() {
 @Override
 public void onResponse(JSONObject response) {
 Log.d("The response", response.toString());
 progressDial.hide();
 JSONArray json = null;
 try {
 json = response.getJSONArray("result");
 } catch (JSONException e) {
 e.printStackTrace();
 }
 try {
 if (json.getString(0).equalsIgnoreCase("0")) {
 Log.d("JsonString: -> ", json.toString());
 progressDial.hide();
 toast();
 } else {
 startagain();
 }
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 error.printStackTrace();
 progressDial.hide();
 }
 }
 );
 jr.add(jsonObject);
asked Jun 21, 2015 at 15:31
3
  • 1
    How is your sever expecting to receive the parameters? Sending post parameters like this makes the post body appear in JSON format, which may not be compatible with your server implementation. Commented Jun 21, 2015 at 15:33
  • @GilMoshayof Okay, thanks. How do i make it compatible please? Commented Jun 21, 2015 at 15:39
  • @GilMoshayof, Using GET worked just fine for me. Thanks a lot for your contribution Commented Jun 25, 2015 at 11:04

2 Answers 2

4

I encountered a similar issue. I had a server API which returned a JSON Object response, so JsonObjectRequest was the go-to request type, but the server didn't like that my body was in JSON format, so I had to make a few changes to my request.

Here's what I did (adapted to your code):

 JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.POST, url, null,
 new Response.Listener<JSONObject>() {
 @Override
 public void onResponse(JSONObject response) {
 Log.d("The response", response.toString());
 progressDial.hide();
 JSONArray json = null;
 try {
 json = response.getJSONArray("result");
 } catch (JSONException e) {
 e.printStackTrace();
 }
 try {
 if (json.getString(0).equalsIgnoreCase("0")) {
 Log.d("JsonString: -> ", json.toString());
 progressDial.hide();
 toast();
 } else {
 startagain();
 }
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 error.printStackTrace();
 progressDial.hide();
 }
 }
 )
 {
 @Override
 public byte[] getBody()
 {
 try
 {
 final String body = "&username=" + username + // assumes username is final and is url encoded.
 "&password=" + password // assumes password is final and is url encoded.
 return body.getBytes("utf-8");
 }
 catch (Exception ex) { }
 return null;
 }
 @Override
 public String getBodyContentType()
 {
 return "application/x-www-form-urlencoded";
 }
 @Override
 public Map<String, String> getHeaders() throws AuthFailureError
 {
 Map<String, String> headers = new HashMap<String, String>();
 headers.put("Accept", "application/json");
 return headers;
 }
 };

Here, I'm not sending any JSON Object as the post body, but instead, I'm creating the post body on my own, form url encoded.

I'm overriding the following methods:

  1. getBody - I'm creating the body of the post exactly the way the server wanted it - form url encoded.
  2. getBodyContentType - I'm telling the server what the content type of my body is
  3. getHeaders - I'm telling the server to return the result in JSON format. This might not be necessary for you.
answered Jun 21, 2015 at 15:42
Sign up to request clarification or add additional context in comments.

4 Comments

I do not really understand. Is there any other way?
did you try the solution I posted?
Yes, i have. Did not work still.Perhaps i didn't do it well. Can't i just use GET instead of POST?
It works great! leave an example here: gist.github.com/doapps/ab1bb202f036d750e5b3
0

If your API return a JSON array, then you should use a JsonArrayRequest, not a JsonRequest.

answered Jul 4, 2016 at 12:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.