0

I run NetworkUsage sample project app on Android Studio, project builds issue free. However, when it runs on my device (NOT emulator), I get an XmlPullParserException. I have my phone connected to wifi and assigned correct permissions in manifest. when i do a debug, i trace it all the way to

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

that is where it stops. The parser processes the inputStream fetching data from

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

for some reason name parameter(in this case "feed") does not equal getName(), in this case html, and i dont know where it is getting html from. My knowledge on parsing xml and hmtl markup is very very limited and going through the motions of learning this aspect of programming and how it will integrate with future projects. i say this because my question might seem trivial to some. Thanks for the help

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 {}

Here is the link to the project/android training section on parsing xml https://developer.android.com/training/basics/network-ops/xml.html

  • You need to change the scheme on `URL` in that sample from `http` to `https`. What you're getting now is a "this has moved" message, which is why the parser fails. https://meta.stackoverflow.com/questions/345012/https-its-time. – Mike M. Nov 30 '17 at 11:31
  • @MikeM. works like a charm mate, i wish Stack Overflow had a friend system so i can add you on my friendlist – Alyosha_Karamazov Nov 30 '17 at 21:54
  • Ah, that's good to hear. I just happened to have downloaded that sample a coupla days when somebody else asked something about it, and I still had it hanging around. Btw, I figured that out by reading the `InputStream` passed into the `parse()` method directly into a `String`, and just `Lod`'d it. There are several methods in [this answer](https://stackoverflow.com/a/35446009) that you can use to do that on Android without any extra setup. #6 is pretty straightforward and portable. Just so ya know, should you need it in the future. Glad you got it working. Cheers! – Mike M. Dec 01 '17 at 04:23

0 Answers0