我想獲取我的當前位置並獲取到某個目的地的駕車路線。 我能得到我的位置,當我執行代碼的行車路線我在下面一行android.os.NetworkOnMainThreadException。如何解決它?
HttpResponse response = httpClient.execute(httpPost, localContext);
響應爲NULL得到android.os.NetworkOnMainThreadException。我該怎麼辦 ?
public Document getDocument(LatLng start, LatLng end, String mode) {
String url = "http://maps.googleapis.com/maps/api/directions/xml?"
+ "origin=" + start.latitude + "," + start.longitude
+ "&destination=" + end.latitude + "," + end.longitude
+ "&sensor=false&units=metric&mode="+mode;
try {
new DownloadWebpageTask().execute(url);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
的[android.os.NetworkOnMainThreadException(http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Simon