-1

I am working on volley library: http://developer.android.com/training/volley/index.html

Get and 'Post methods without parameters' working fine. But when parameters are given, volley does not execute the form, and acts like form itself is a jsonObject:

com.android.volley.ParseError: org.json.JSONException: Value Login< of type java.lang.String cannot be converted to JSONObject

I have tried both overriding getParams() method:

@Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("username", username);
            params.put("password", password);
            return params;
        }

And instantiating the object with parameter:

Map<String, String> params2 = new HashMap<String, String>();
    params2.put("username", username);
    params2.put("password", password);

    JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.POST, LOGIN_URL, new JSONObject(params2), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            //onResponse
        }
    });

None of them worked. I am guessing my problem is about the content types. Volley library uses application/json while my php codes use name-value pairs.

I have seen these two questions but sadly they did not solve my case:

Google Volley ignores POST-Parameter

Volley Post JsonObjectRequest ignoring parameters while using getHeader and getParams

Community
  • 1
  • 1
starkm
  • 859
  • 1
  • 10
  • 21

1 Answers1

0

When you use JsonObjectRequest, you are saying that the content you are posting is a JSON Object and the response you expect back will also be a JSONObject. If neither of these are true, you need to build your own Request<T> and set the values you need.

The error you are seeing is because the response from the server is not a valid JSON response.

Jeffrey Mixon
  • 12,846
  • 4
  • 32
  • 55
  • I did post JSON and expected JSON. So I should use StringRequest because my form accepts namevalue pairs? – starkm Nov 06 '14 at 00:29
  • Maybe. It depends on what `Content-Type` the server is expecting, and exactly what the response is going to be. You can override `getBodyContentType` to change the request `Content-Type` if you need to post it as, say `application/x-www-form-urlencoded` instead of a JSON `Content-Type` – Jeffrey Mixon Nov 06 '14 at 00:34
  • As JsonObjectRequest sending application/json by default. Overriding it may solve my problem. I'll give it a go. Thanks – starkm Nov 06 '14 at 00:38