zgc7009 zgc7009 - 1 year ago 63
Android Question

Issue streaming directions for Google Maps API v2 Android

So part of my application builds a navigation directions string and then attempts to parse the JSON and draw the polyline routes on my map. I first build my string using Location variables or Locale constants. I end up with something like

https://maps.googleapis.com/maps/api/directions/json?origin=Full Frame Documentary Film
Festival, Durham, 27701&destination=601 W Peace St, Raleigh,27605&sensor=false&key={API_KEY}

  • there is no new line (I added it for readability) and the {API_KEY} is my actual api key.

The issue that I am running into is that when I pass that URL String to this downloadUrl(String urlString) method

private String downloadUrl(String urlString) throws IOException {
Log.d(TAG, "Downloaded string = " + urlString);
String data = "";
InputStream stream = null;
HttpURLConnection urlConnection = null;
try {

// Display our JSON in our browser (to show us how we need to implement our parser)
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(urlString));

URL url = new URL(urlString);

// Create a http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();

// read in our data
stream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
StringBuffer sb = new StringBuffer();

// read in our data in, and append it as a single data string
String line = "";
while ((line = br.readLine()) != null) {
Log.d(TAG,"url download stream: " + line);
data = sb.toString();
catch (Exception e) {
finally {
Log.d(TAG, "Downloaded data = " + data);

return data;

the JSON displays in my browser correctly, I see everything as Google describes it in the documentation. But then in the following lines when I try to open a connection to the URL and pull the JSON into a string for parsing, I get the System.err notification

05-02 09:56:01.540: W/System.err(32232): java.io.FileNotFoundException:
https://maps.googleapis.com/maps/api/directions/json?origin=Full Frame Documentary
Film Festival, Durham, 27701&destination=601 W Peace St, Raleigh, 27605&sensor=false&key={API_KEY}

I guess my confusion comes in the fact that the browser displays the parsed address perfectly, but then the connection to (what I believe is) the same server returns a FNFE. Am I wrong in assuming that this is the case? If so might my key actually be wrong? The confusing thing is that this code works in another application.

Answer Source

You have to URL-encode the params, e.g. a space (" ") in a URL is written as "+". Your browser internally does this, probably without showing you the submitted URL.

static String urlEncode(String value) {
    try {
        return URLEncoder.encode(value, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        return value;

But don't encode the whole URL, only the parameter values. If the parameter names are non-ASCII, they have to be encoded as well, but Google APIs don't use such parameters.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download