0

I'm trying to connect my Android app to my Laravel API and I'm receiving the following error.

Value string(2) of type java.lang.String cannot be converted to JSONObject

after running my volley request. The request goes trough, but I cannot check for status of the request because it always returns an error.

I believe it is because I'm returning a JSON Array in my Laravel app.

This is my Laravel's app response on success

return Response::json(array(
                                'status'  => 'ok',
                                'message'  => 'success',
                        ));

And this is my Volley request on Android

RequestQueue queue = Volley.newRequestQueue(this);
        Log.d("sendOrderToServer", "called");
        Log.d("json obj", order.toString());


        JsonObjectRequest jsor = new JsonObjectRequest(MainActivity.ADD_ORDER, order, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("response", response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("error34", error.toString());
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json");
                headers.put("charset", "utf-8");
                return headers;
            }
        };
        queue.add(jsor);

How could I do this properly without manually parsing a string and making a string request in Android Volley library?

Petar-Krešimir
  • 444
  • 8
  • 22

2 Answers2

1

Try using " (double quote) instead ' (single quote)

JSON "doesn't like" single quote, double quote is standard:

return Response::json(array(
                                "status"  => "ok",
                                "message"  => "success",
                        ));

See on SO: jQuery.parseJSON single quote vs double quote

Community
  • 1
  • 1
Sasa Jovanovic
  • 324
  • 2
  • 14
  • 1
    Thank you, will change it. I found out what was the issue, I had a Laravel event being created before the return, and it was returnin the event's broadcastOn return value as well, so the response was a string + json and that's why Volley was failing. – Petar-Krešimir Apr 08 '17 at 10:51
0

I had a var_dump in my event' __construct for debugging purposes and I forgot about it.

Petar-Krešimir
  • 444
  • 8
  • 22