Google Directions API を使用して、Android アプリのルートから JSON 情報を取得したいと考えています。しかし、ステータスコードとして常に「NOT_FOUND」が表示されます。ブラウザにリクエストと同じ URL を入力すると、正常に動作します。
ここで、AsyncTask 内の私のコード:
@Override
protected String doInBackground(final String... params) {
String startAddress = params[0];
String finishAddress = params[1];
departureTime = params[2];
String travelMode = params[3];
String result = "";
String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + startAddress
+ "&destination=" + finishAddress + "&departure_time=" + departureTime + "&mode=" + travelMode + "&sensor=true";
url = url.trim().replace(' ', '+');
Log.d(TAG, "URL String: " + url);
try {
URL urlObject = new URL(url);
URLConnection urlConn = urlObject.openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(urlConn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
result = result + inputLine;
}
in.close();
return result;
} catch (Exception e) {
exception = e.toString();
Log.e(TAG, "Exception getting route from Google Directions API: " + e.toString());
Logger.e(context, TAG, "Exception getting route from Google Directions API: " + e.toString());
return null;
}
}
出力は次のようになります。
02-23 08:54:01.811 25055-25055/de.bmw.ceprouteguide D/PlanRouteActivity﹕ startAddress: Hanauer Straße 46, München, endAddress: Westendstraße 11, München, travelMode: transit, date: 23-02-2015, time: 08:54
02-23 08:54:01.812 25055-25055/de.bmw.ceprouteguide D/PlanRouteActivity﹕ Millis since epoch:1424678040, date string: 23-02-2015 08:54
02-23 08:54:01.816 25055-25618/de.bmw.ceprouteguide D/PlanRouteActivity﹕ URL String: http://maps.googleapis.com/maps/api/directions/json?origin=Hanauer+Straße+46,+München&destination=Westendstraße+11,+München&departure_time=1424678040&mode=transit&sensor=true
02-23 08:54:02.116 25055-25055/de.bmw.ceprouteguide D/PlanRouteActivity﹕ Route: { "routes" : [], "status" : "NOT_FOUND"}
02-23 08:54:02.117 25055-25055/de.bmw.ceprouteguide D/PlanRouteActivity﹕ Google Directions status code: NOT_FOUND
誰でも問題がどこにあるのか考えていますか?