Directions post request

5 posts / 0 new
Last post
Daishun
Directions post request

I am trying to make a POST Request to the Route API (using Java for Android) but the API is returning an error saying:

"A JSONObject text must begin with '{' at character 0 of ,  Please see the documentation...."

The error message does not even complete the sentence after "0 of ,". My request String does start with a **{** but the documentation offers no more help. My request String is:

    {"locations":["50.1234567,-90.1234567","50.1234567,-90.1234567"],"options":{"tryAvoidLinkIds":[],"unit":"k","routeType":"pedestrian","maxLinkId":"1","fullShape":"true"}}

I have no tryAvoidLinkIds at this stage. My code to make the request is:

        // Make the request data
        JSONObject request = null;
        try {
            // JSONObject to hold everything
            request = new JSONObject();

            // JSONArray of start and finish
            JSONArray locations = new JSONArray();
            locations.put(mOrigin);
            locations.put(mDestination);
            request.put("locations", locations); // put array inside main object

            // JSONObject options
            JSONObject options = new JSONObject();

            // Array of links numbers to avoid
            JSONArray tryAvoidLinkIds = new JSONArray();
            for (int link : myLinks) {
                tryAvoidLinkIds.put(link);
            }
            options.put("tryAvoidLinkIds", tryAvoidLinkIds); // put array inside options object

            options.put("unit", "k");
            options.put("routeType", "pedestrian");
            options.put("maxLinkId", "1");
            options.put("fullShape", "true");

            request.put("options", options); // put options object inside main object
        }
        catch (JSONException e) {
            e.printStackTrace();
        }

Hopefully someone knows what I am doing wrong here? I know how to parse JSON but this is my first time making a request in JSON as before I was using a GET request. I am trying post because I need to send hundreds of link Ids and the get request is just hanging. I can't find any information about a limit to tryAndAvoidLinkIDs. Please see the example request body at https://developer.mapquest.com/documentation/directions-api/route/post/.


MQBrianCoakley
It almost seems like there is
It almost seems like there is no json getting sent in the post body since the error says "0 of ,". 

Daishun
It looks like you are right

It looks like you are right but I don't get any exception in the catch block. This is the whole code with the API call. Please can you take a look and tell me what I am doing wrong?

http://pasted.co/e81f9fdf


Daishun
I fixed it when I finally

I fixed it when I finally found a working example online. Someone had the same problem. I added these request properties to my code:

connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");

My pasted code was missing connection.setRequestMethod("POST") but I had actually included that before.

I also used DataOutputStream instead of OutputStreamWriter but I don't think this matters.


MQBrianCoakley
Thanks for posting your
Thanks for posting your solution!