我正在应用XML Parser来使用此URL上的XML文件:http://www.emmebistudio.com/markers.xml以将地图的标记数据保存到Android应用程序中。这是我应该解析它的代码,但是使用eclipse调试器我已经看到它在第二行返回connected = false,所以,我在哪里错了?
try{
URL url = new URL("http://www.emmebistudio.com/markers.xml");
URLConnection conn = url.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
//Document doc = builder.parse(conn.getInputStream());
NodeList markers = doc.getElementsByTagName("marker");
for (int i = 0; i < markers.getLength(); i++) {
Element item = (Element) markers.item(i);
String name = item.getAttribute("name");
String address = item.getAttribute("address");
String stringLat = item.getAttribute("lat");
String stringLong = item.getAttribute("long");
String icon = item.getAttribute("icon"); //assigned variable for the XML icon attribute
Double lat = Double.valueOf(stringLat);
Double lon = Double.valueOf(stringLong);
map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions()
.position(new LatLng(lat, lon))
.title(name)
.snippet(address));
}
}catch (Exception e){
// If I can't connect to the file I only see the map with my position
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
e.printStackTrace();
}
答案 0 :(得分:0)
您可能需要添加Internet访问。
将INTERNET权限添加到清单文件中。
你必须添加这一行:
<uses-permission android:name="android.permission.INTERNET" />
在AndroidManifest.xml中的应用程序标记之外