我遇到了一些空指针异常错误消息的问题。基本上我有一个方法返回Document对象,我的其他方法将使用Document对象作为参数。
以下是我上网的参考资料:
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 {
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;
}
当我测试URL时,它确实返回了一些东西。但是,当它执行此方法时,它只是继续为Document对象返回null。有人可以向我解释一下代码的Http部分是什么吗?我很困惑。
提前致谢。
已编辑的部分
LatLng fromPosition = new LatLng(1.3564, 103.8311);
LatLng toPosition = new LatLng(1.3426, 103.9254);
map = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
GMapV2Direction md = new GMapV2Direction();
Document doc = md.getDocument(fromPosition, toPosition, GMapV2Direction.MODE_DRIVING);
System.out.println(doc);
map.addMarker(new MarkerOptions().position(fromPosition).title("Start"));
map.addMarker(new MarkerOptions().position(toPosition).title("End"));
ArrayList<LatLng> directionPoint = md.getDirection(doc);
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}
map.addPolyline(rectLine);