XML 파서를 적용하여 XML 파일 (이 URL의 http://www.emmebistudio.com/markers.xml)을 적용하여지도의 마커 데이터를 Android 앱에 보관합니다. 이 코드를 구문 분석해야하지만 일식 디버거를 사용하면 두 번째 줄에 연결됨 = false가 반환되는 것을 보았습니다. 그래서 어디서 잘못 되었습니까?XML 파서가 연결을 열지 않는 이유는 무엇입니까?
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();
}
당신이 UI 스레드에서 그 조각 실행하고 당신의 AndroidManifest.xml에서 응용 프로그램 태그 외부
:
이 줄을 추가해야? – Blackbelt
예. 시도 할 때만 .. 원활하게 실행되는 경우 Java 클래스를 수행합니다. –
실제로 문제입니다. 아마도 NetworOnMainThreadException을 얻고있을 것입니다. 그러나 stacktrace를 인쇄하지 않으므로 정확히 무슨 일이 벌어지고 있는지 말하기는 어렵습니다. – Blackbelt