2013-01-24 88 views
0

我有以下類:如何通過url獲取xml(AsyncTask,android)?

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     XmlManager xm = new XmlManager(); 
     Log.i("tag",documentToString(xm.getXmlFromUrl("http://domen.com/abc/timelist.xml"))); 
    } 
    //................ 
} 

public class XmlManager { 
    public Document getXmlFromUrl(String url) { 
     GetXmlTask txt = new GetXmlTask(); 
     txt.execute(url); 
     Document doc = null; 
     try { 
      doc = txt.get(5, TimeUnit.SECONDS); 
      Log.i("tag", "test"); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return doc; 
    } 
    //................ 
} 

public class GetXmlTask extends AsyncTask<String, Void, org.w3c.dom.Document> { 
    @Override 
    protected Document doInBackground(String... arg0) { 
      String url = ""; 
       if(arg0.length > 0){ 
        url = arg0[0];     
       } 

       Document doc = null; 
      try { 
      doc = stringToDom(convertStreamToString(new URL(url).openStream())); 
      } 
      catch (Exception e) { 
      e.printStackTrace(); 
      } 

      return doc; 
    } 
    //................ 
} 

不幸的是,我不明白的地方存在錯誤。我在logcat中看到(沒有過濾器):

01-24 17:47:44.194:W /合子(37):預裝繪製資源 與配置而有所不同!! 01-24 17:47:44.204:W /合子(37): 預裝繪製資源#0x1080446 (RES /抽拉-MDPI/popup_inline_error_above_holo_light.9.png),其與 配置而變化! 01-24 17:47:44.284:W/Zygote(37): 預裝的可繪製資源#0x1080447 (res/drawable-mdpi/popup_inline_error_holo_dark.9.png),其配置變化爲 ! 01-24 17:47:44.294:W/Zygote(37):預加載 可繪製資源#0x1080448 (res/drawable-mdpi/popup_inline_error_holo_light.9.png)變化 配置! 01-24 17:47:46.315:W/Zygote(37):預加載 drawable資源#0x10802ec (res/drawable-mdpi/ic_lockscreen_outerring.png)隨 配置而變化! 01-24 17:47:48.644:A/BatteryStatsImpl(93): libcore.io.ErrnoException:打開失敗:ENOENT(沒有這樣的文件或目錄 )01-24 17:47:48.714:E/AndroidRuntime致(93):錯誤報告 WTF 01-24 17:47:48.714:E/AndroidRuntime(93):在 android.util.Log $ 1.onTerribleFailure(Log.java:103)01-24 17:47:56.774: E /的Soundpool(93):錯誤裝載/system/media/audio/ui/Lock.ogg 01-24 17:47:56.774:E /的Soundpool(93):錯誤裝載 /系統/媒體/音頻/ UI /Unlock.ogg 01-24 17:48:03.064: W/NetworkManagementSocketTagger(93):setKernelCountSet(10005,1) 失敗,錯誤號-2 01-24 17:48:11.094:d/StrictMode(190):在 com.android.internal.telephony.gsm.SpnOverride.loadSpnOverrides(SpnOverride .java:61) 01-24 17:48:11.094:D/StrictMode(190):at com.android.internal.telephony.gsm.SpnOverride。(SpnOverride.java:43) 01-24 17:48 :12.592:E/SoundPool(93):錯誤加載 /system/media/audio/ui/Effect_Tick.ogg 01-24 17:48:12.592: E/SoundPool(93):error loading/system/media/audio /ui/Effect_Tick.ogg 01-24 17:48:12.592:E/SoundPool(93):錯誤加載 /system/media/audio/ui/Effect_Tick.ogg 01-24 17:48:12.592: E /的Soundpool(93):錯誤裝載/system/media/audio/ui/Effect_Tick.ogg 01-24 17:48:12.592:E /的Soundpool(93):錯誤裝載 /系統/媒體/音頻/ UI/Effect_Tick。 ogg 01-24 17:48:12.592: E/SoundPool(93):錯誤加載 /system/media/audio/ui/KeypressStandard.ogg 01-24 17:48:12.592: E /的Soundpool(93):錯誤裝載 /system/media/audio/ui/KeypressSpacebar.ogg 01-24 17: (93):錯誤加載 /system/audio /媒體/音頻/ UI/KeypressReturn.ogg 01-24 17:48:12.982: W/AudioService(93):loadSoundEffects(),錯誤-1而裝載樣品 01-24 17:48:20.262:A/NetworkStats( 93):導致: libcore.io.ErrnoException:打開失敗:ENOENT(沒有這樣的文件或 目錄)01-24 17:48:24.822:D/StrictMode(190):在 com.android.internal.telephony .gsm.GsmDataConnectionTrack er.getPreferredApn(GsmDataConnectionTracker.java:2276) 01-24 17:48:28.822:A/NetworkStats(93):引起者: libcore.io。ErrnoException:打開失敗:ENOENT(無此文件或 目錄)01-24 17:48:30.883:D/MmsSmsDatabaseHelper(190):[MmsSmsDb] hasAutoIncrement:CREATE TABLE threads(_id INTEGER PRIMARY KEY AUTOINCREMENT,date INTEGER DEFAULT 0,message_count INTEGER DEFAULT 0,recipient_ids TEXT,snippet TEXT,snippet_cs INTEGER DEFAULT 0,讀取 INTEGER DEFAULT 1,類型INTEGER DEFAULT 0,錯誤INTEGER DEFAULT 0,has_attachment INTEGER DEFAULT 0)result:true 01-24 17:48 :38.492: W/MediaScanner(412):錯誤打開目錄 '/mnt/sdcard/.android_secure/',跳過:權限被拒絕。 01-24 17:48:44.462:I/ActivityManager(93):啓動proc com.svox.pico for broadcast com.svox.pico/.VoiceDataInstallerReceiver:pid = 541 uid = 10024 gids = {1015} 01- 24:17:48:51.142:W/NetworkManagementSocketTagger(93): setKernelCountSet(10041,1)失敗,並且errno -2 01-24 17:48:55.792: W/NetworkManagementSocketTagger(93):setKernelCountSet(10005,0) errno失敗-2

按應用程序名稱篩選: LogCat中存在空白。

過濾器的標籤 「標籤」:

01-24 17:48:03.064:W/NetworkManagementSocketTagger(93): setKernelCountSet(10005,1)失敗,錯誤號-2 01-24 17: 48:51.142: W/NetworkManagementSocketTagger(93):setKernelCountSet(10041,1) 失敗,錯誤號-2 01-24 17:48:55.792: W/NetworkManagementSocketTagger(93):setKernelCountSet(10005,0) 失敗errno -2

回答

3

看起來好像是XML文件沒有被正確解析用於你的應用程序。解決這個問題的一個可能的解決方案是創建一個合適的XML解析器,然後使用它來遍歷AsyncTask中文件中的所有節點。這裏是一個解析器我創建了一個例子:

接下來,你要做的就是使用解析器來獲取DOM元素,推動在任何你想要顯示它的信息在onPostExecute()方法中。下面是一個示例,

........ 

private class LoadExampleTask extends 
     AsyncTask<Void, Integer, ArrayList<HashMap<String, String>>> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // code here 
    } 

    @Override 
    protected ArrayList<HashMap<String, String>> doInBackground(
      Void... params) { 

     XMLParser parser = new XMLParser(); // the parser create as seen in the Gist from GitHub 
     String xml = parser.getXmlFromUrl(URL); // getting XML from URL 
     Document doc = parser.getDomElement(xml); // getting DOM element 

     NodeList nl = doc.getElementsByTagName(KEY_VENUE); 

     // looping through all song nodes <venue> 
     for (int i = 0; i < nl.getLength(); i++) { 
      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 
      Element e = (Element) nl.item(i); 
      // adding each child node to HashMap key => value 
      map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
      map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE)); 
      map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION)); 
      map.put(KEY_LOCATION, parser.getValue(e, KEY_LOCATION.toString())); 
      map.put(KEY_TAGS, parser.getValue(e, KEY_TAGS.toString())); 
      map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL)); 
      map.put(KEY_THUMB_LARGE_URL, parser.getValue(e, KEY_THUMB_LARGE_URL)); 

      // adding HashList to ArrayList 
      exampleList.add(map); 
     } 

     return exampleList; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
     super.onPostExecute(result); 

     try { 
      // Getting adapter by passing xml data ArrayList 
      adapter = new YourExampleAdapter(getActivity(), exampleList); 
      list.setAdapter(adapter); 
      adapter.notifyDataSetChanged(); 

     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

........ 

此外,這是一個例子使用這種方法的XML文件的結構,但任何結構應該是罰款,只要你正確映射鍵:

<venues> 
    <venue> 
     <id></id> 
     <title></title> 
     <location></location> 
     <tags></tags> 
     <description></description> 
     <thumb_url></thumb_url> 
     <thumb_large_url></thumb_large_url> 
    </venue> 
</venues> 

希望這有助於解決您的錯誤。

+0

非常感謝! – Denis

+0

@Denis您的歡迎。 –

相關問題