谷歌地图API第2版的Android发行流方向
所以我的应用程序的一部分,建立一个导航方向字符串,然后试图解析JSON和借鉴我的地图中的折线路线。我先用建位置的变量或常量的Locale我的字符串。我结束了类似
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}
- 有没有新线(我加了它的可读性)和{} API_KEY是我的实际API密钥。
这是我遇到的问题是,当我传递URL字符串我downloadUrl(字符串urlString)方法
The issue that I am running into is that when I pass that URL String to my 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));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
URL url = new URL(urlString);
// Create a http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
// 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);
sb.append(line);
}
data = sb.toString();
br.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
Log.d(TAG, "Downloaded data = " + data);
stream.close();
urlConnection.disconnect();
}
return data;
}
问题是,在我的浏览器的JSON显示正确,我看到的一切,因为谷歌将其描述的文件中。但随后在以下几行,当我尝试打开到URL的连接,并拉成JSON解析字符串,我得到的通知System.err的
The thing is, 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}
我想我的困惑来自于一个事实,即浏览器完美显示解析的地址,但随后的连接(我认为是)在同一台服务器返回一个FNFE。我错在假设是这种情况?如果是的话可能我的钥匙实际上是错的?令人困惑的是,这个code在其他应用程序的工作。
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.
您必须进行URL连接code中的参数,可以如一个空格()中的URL被写入为+。您的浏览器内做到这一点,很可能没有表现出你提交的URL。
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;
}
}
但不要带code整个URL,只有参数值。如果参数名非ASCII字符,它们必须连接codeD一样好,但谷歌的API不使用这样的参数。
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.