2014-09-22 2 views
1

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(); 
} 
+0

당신이 UI 스레드에서 그 조각 실행하고 당신의 AndroidManifest.xml에서 응용 프로그램 태그 외부

<uses-permission android:name="android.permission.INTERNET" /> 

:

이 줄을 추가해야? – Blackbelt

+0

예. 시도 할 때만 .. 원활하게 실행되는 경우 Java 클래스를 수행합니다. –

+2

실제로 문제입니다. 아마도 NetworOnMainThreadException을 얻고있을 것입니다. 그러나 stacktrace를 인쇄하지 않으므로 정확히 무슨 일이 벌어지고 있는지 말하기는 어렵습니다. – Blackbelt

답변

0

인터넷 액세스를 추가해야 할 수도 있습니다.

매니페스트 파일에 인터넷 권한을 추가하십시오.

+0

메인 스레드의 인터넷 액세스가 허용되지 않습니다. – RichieHH

+0

이 권한이 이미 있습니다. @RichieHH –

관련 문제