我写了应用程序的代码,只是学习如何做到这一点。我在1月份完成了它并且它工作了。该应用程序应该在Google地图上找到给定的路径。它曾经工作到上周末。昨天我尝试了它,它不起作用。我的意思是,似乎应用程序无法询问Google服务器的路径。代码是:
public class asker {
public String metodo(Activity act,String stringUrl) throws IOException, JSONException{
String polyline="";
JSONArray array;
StringBuilder response = new StringBuilder();
URL url = new URL(stringUrl);
try{
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
//httpconn.setConnectTimeout (4000);
//httpconn.setReadTimeout (4000);
if ((act!=null)&&httpconn.getResponseCode() == HttpURLConnection.HTTP_OK){
BufferedReader input = new BufferedReader(
new InputStreamReader(httpconn.getInputStream()),
8192);
String strLine = null;
while ((strLine = input.readLine()) != null) {
response.append(strLine);
}
input.close();
}
String jsonOutput = response.toString();
JSONObject jsonObject = new JSONObject(jsonOutput);
// routesArray contains ALL routes
JSONArray routesArray = jsonObject.getJSONArray("routes");
// Grab the first route
JSONObject route = routesArray.getJSONObject(0);
JSONObject poly = route.getJSONObject("overview_polyline");
polyline = poly.getString("points");
} catch (ClientProtocolException e) {//devo gestire le eccezioni anche qui
//per evitare lo shut down della activity tappa
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return polyline;
}
}
过去一直工作到上周末。我错过了关于地图API的新内容吗? 先感谢您。
logcat的:
03-06 18:12:09.570: W/System.err(10908): org.json.JSONException: Index 0 out of range [0..0)
03-06 18:12:09.570: W/System.err(10908): at org.json.JSONArray.get(JSONArray.java:282)
03-06 18:12:09.575: W/System.err(10908): at org.json.JSONArray.getJSONObject(JSONArray.java:510)
03-06 18:12:09.575: W/System.err(10908): at com.example.senarumvinea.asker.metodo(asker.java:44)
03-06 18:12:09.575: W/System.err(10908): at com.example.senarumvinea.Tappa2$GetDirection.doInBackground(Tappa2.java:360)
03-06 18:12:09.575: W/System.err(10908): at com.example.senarumvinea.Tappa2$GetDirection.doInBackground(Tappa2.java:1)
03-06 18:12:09.575: W/System.err(10908): at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-06 18:12:09.575: W/System.err(10908): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-06 18:12:09.575: W/System.err(10908): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-06 18:12:09.575: W/System.err(10908): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-06 18:12:09.575: W/System.err(10908): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-06 18:12:09.575: W/System.err(10908): at java.lang.Thread.run(Thread.java:811)