DirectionResult has null routes and waypoints - Google Directions API

2k views Asked by At

My goal for this program was to poll the Google Directions API and plot the course by Polyline on a MapView in an Android app.

However, when I get the DirectionsResult back from the API call, trying to access directionsResult.routes[0] or directionsResult.geocodedWaypoints[0] results in a NullPointerException.

I am currently importing the following libraries using maven repositories (excerpt from my build.gradle):

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.http-client:google-http-client-android:1.21.0'
compile 'com.google.http-client:google-http-client-jackson:1.21.0'
compile 'com.google.http-client:google-http-client-jackson2:1.21.0'
compile 'com.google.http-client:google-http-client-gson:1.21.0'
compile 'com.google.http-client:google-http-client-protobuf:1.21.0'
compile 'com.google.http-client:google-http-client:1.21.0'
compile 'com.google.maps:google-maps-services:0.1.10'
compile 'com.google.android.gms:play-services:8.4.0'

My current code inside an AsyncTask implementation with debugging calls commented out:

@Override
protected synchronized String doInBackground(URL... params)
{
    try {
        HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest request) throws IOException {
                request.setParser(new JsonObjectParser(JSON_FACTORY));
            }
        });

        GenericUrl url = new GenericUrl("http://maps.googleapis.com/maps/api/directions/json");
        // to and from are both LatLng's
        url.put("origin", from.toUrlValue());
        url.put("destination", to.toUrlValue());
        url.put("sensor", true);
        HttpRequest request = requestFactory.buildGetRequest(url);
        //wait(1000);
        HttpResponse httpResponse = request.execute();
        //Log.i(TAG, httpResponse.parseAsString());
        //wait(1000);
        DirectionsResult directionsResult = httpResponse.parseAs(DirectionsResult.class);
        //wait(1000);
        //Log.i(TAG, Integer.toString(httpResponse.getStatusCode()));
        //Log.i(TAG, httpResponse.getStatusMessage());
        latlngs = directionsResult.routes[0].overviewPolyline.decodePath();

    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

My Debugging Process:

  1. This is my printed request url:

    http://maps.googleapis.com/maps/api/directions/json?origin=40.426870,-86.925083&destination=40.430092,-86.921679&sensor=true

Obviously it is a short but complete call. With elements of legs containing polyline points (which is what I assume I'm after). This JSON is the same as the response I got from reading httpResponse.parseAsString().

  1. Looking at some StackExchange questions, I saw some people suggested waiting for data to be received. I put 1 second delays between all integral parts of request cycle with no results.

  2. None of the other parts of the call were null.

  3. httpResponse.getStatusCode() returned 200.

  4. httpResponse.getStatusMessage() returned OK.

Everything appears normal until I attempt to use DirectionsResult to parse the JSON HttpResponse with:

DirectionsResult directionsResult = HttpResponse.parseAs(DirectionsResult.class);

After which I get a NullPointerException when accessing the two fields of DirectionsResult: routes and geocodedWaypoints.

Has anyone had similar issues with this class? I've been thinking I might submit an issue on the proper google-http-client library GitHub page if this isn't resolved here.

1

There are 1 answers

1
bmaxfie On

So it turns out that there were two things I was not doing right.

First, I included no API key in the URL, which doesn't explain why I was able to print a correctly formatted JSON response with httpRequest.parseAsString(), but regardless obviously an API key would need to be included for proper billing of my Google Developer account. Another change that needed to be made as a result of this, is changing the URL from 'http' to 'https'.

Second, there seems to be an issue with requesting an Android API key rather than a Server API key. I received this response when using the Android API key:

 {
    "error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address 128.210.106.49, with empty referer",
    "routes" : [],
    "status" : "REQUEST_DENIED"
}

As a solution, when creating an API key, choose the Server option instead of Android.