NetworkUsage示例应用程序显示错误解析XML

时间:2017-11-30 11:00:22

标签: android

我在Android Studio上运行NetworkUsage示例项目应用程序,项目构建免费问题。但是,当它在我的设备(非模拟器)上运行时,我得到一个 XmlPullParserException 。我将手机连接到wifi并在清单中分配了正确的权限。当我进行调试时,我一直追踪到

parser.require(XmlPullParser.START_TAG, ns, "feed");  

这就是它停止的地方。解析器处理 inputStream

获取数据
private static final String URL = "http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest";  

由于某种原因,name参数(在本例中为“feed”)不等于getName(),在本例中为 html ,我不知道它在哪里 html 来自。我对解析xml和hmtl标记的知识非常有限,并且经历了学习编程的这一方面以及它将如何与未来项目集成的动作。我这样说是因为我的问题对某些人来说似乎微不足道。谢谢你的帮助

public class StackOverflowXmlParser {
private static final String ns = null;

// We don't use namespaces

public List<Entry> parse(InputStream in) throws XmlPullParserException, IOException {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        return readFeed(parser);
    } finally {
        in.close();
    }
}

private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    List<Entry> entries = new ArrayList<Entry>();

    parser.require(XmlPullParser.START_TAG, ns, "feed");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        // Starts by looking for the entry tag
        if (name.equals("entry")) {
            entries.add(readEntry(parser));
        } else {
            skip(parser);
        }
    }
    return entries;
}

// This class represents a single entry (post) in the XML feed.
// It includes the data members "title," "link," and "summary."
public static class Entry {....}

// Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them
// off
// to their respective &quot;read&quot; methods for processing. Otherwise, skips the tag.
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {}

// Processes title tags in the feed.
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {}

// Processes link tags in the feed.
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {}

// Processes summary tags in the feed.
private String readSummary(XmlPullParser parser) throws IOException, XmlPullParserException {}

// For the tags title and summary, extracts their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {}

// Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e.,
// if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it
// finds the matching END_TAG (as indicated by the value of "depth" being 0).
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {}

这是解析xml的项目/ android培训部分的链接 https://developer.android.com/training/basics/network-ops/xml.html

0 个答案:

没有答案