0

我有以下课程:

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/Zygote(37):预加载的可绘制资源因配置而异!!01-24 17:47:44.204: W/Zygote(37): 预加载的可绘制资源 #0x1080446 (res/drawable-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): 预加载的可绘制资源 #0x10802ec (res/drawable-mdpi/ic_lockscreen_outerring.png) 随配置而变化!!01-24 17:47:48.644:A/BatteryStatsImpl(93):原因:libcore.io.ErrnoException:打开失败:/mnt/sdcard/.android_secure/',跳过:权限被拒绝。01-24 17:48:44.462: I/ActivityManager(93): 启动 proc com.svox.pico 广播 com.svox.pico/.VoiceDataInstallerReceiver: pid=541 uid=10024 gids={1015} 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) 失败,错误号 -2

按应用程序名称过滤:LogCat 中有 void。

按标签“标签”过滤:

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) 失败使用 errno -2 01-24 17:48:55.792: W/NetworkManagementSocketTagger(93): setKernelCountSet(10005, 0) 失败,出现 errno -2

4

1 回答 1

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>

希望这有助于解决您的错误。

于 2013-01-24T19:10:05.917 回答