简体   繁体   中英

Android error while creating table from parsed XML file

I'm trying to parse an XML file which contains all episodes from 1 tv show. Whenever I try to put the result from the XML file via the domparser into a List I get an error. I guess this is because it works async, but how can I solve this issue or what am I doing wrong?

Later on I want to put the data of this list into the database as you can see, I left it commented out for now.

Code of my the onCreate where I call the method readEpisodeListXmlFromSerieId(Long id) to parse the XML file:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    db = new DatabaseHelper(this);
    shows = db.getAllMyFollowingShows();

    if (shows.size() != 0) {
        for (Show show : shows) {
            if (db.getAllEpisodesFromShow(show.getId()).size() == 0) {
                readEpisodeListXmlFromSerieId(show.getId());
            }
        }
    }
}

Here is the code I parse the XML file and try to put the data into the list:

private void readEpisodeListXmlFromSerieId(Long id) {
    HttpReader httpReader = new HttpReader();

    httpReader.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
                @Override
                public void resultReady(String result) {
                    domParser.buildDomDocument(result);

                                            // This gives the error
                    episodesFromShow = domParser.getAllEpisodesByShow();

//                      for (Episode episode : episodesFromShow) {
//                          try {
//                              db.createEpisode(episode);
//                          } catch (Exception e) {
//                              // TODO: handle exception
//                          }   
//                      }
                }

            });
    httpReader.execute("http://thetvdb.com/data/series/" + id + "/all/");
}

This is the function in the domparser which is called to fill the list:

public List<Episode> getAllEpisodesByShow() {
    List<Episode> list = new ArrayList<Episode>();
    NodeList episodes = doc.getDocumentElement().getElementsByTagName(
            "Episode");

    for (int i = 0; i < episodes.getLength(); i++) {
        String aired = episodes.item(i).getChildNodes().item(11)
                .getTextContent();

        if (!aired.equals("")) {
            list.add(new Episode(Long.parseLong(episodes.item(i)
                    .getChildNodes().item(0).getTextContent()), Integer
                    .parseInt(episodes.item(i).getChildNodes().item(10)
                            .getTextContent()), episodes.item(i)
                    .getChildNodes().item(9).getTextContent(), episodes
                    .item(i).getChildNodes().item(15).getTextContent(),
                    Integer.parseInt(episodes.item(i).getChildNodes()
                            .item(19).getTextContent()), Integer
                            .parseInt(episodes.item(i).getChildNodes()
                                    .item(28).getTextContent()), aired, 0));

        }
    }
    return list;
}

Here is the error from logcat:

FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid int: ""
java.lang.Integer.invalidInt(Integer.java:138)
java.lang.Integer.parseInt(Integer.java:359)
java.lang.Integer.parseInt(Integer.java:332)
com.example.myshows.DomParser.getAllEpisodesByShow(DomParser.java:107)
com.example.myshows.MyShows$1.resultReady(MyShows.java:50)
com.example.myshows.HttpReader.onPostExecute(HttpReader.java:40)
com.example.myshows.HttpReader.onPostExecute(HttpReader.java:1)
android.os.AsyncTask.finish(AsyncTask.java:602)
android.os.AsyncTask.access$600(AsyncTask.java:156)
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4524)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
dalvik.system.NativeStart.main(Native Method)

It looks like your code is trying to parse an empty string as an integer. Check your input for empty strings.

Also, this will most likely cause an "Application Not Responding" error because you are running the parsing job on the main thread. See Best Practices for Background Jobs for ways to push it onto its own thread.

Your error occurs here:

    if (!aired.equals("")) {
        list.add(new Episode(Long.parseLong(episodes.item(i)
                .getChildNodes().item(0).getTextContent()), Integer
                .parseInt(episodes.item(i).getChildNodes().item(10)
                        .getTextContent()), episodes.item(i)
                .getChildNodes().item(9).getTextContent(), episodes
                .item(i).getChildNodes().item(15).getTextContent(),
                Integer.parseInt(episodes.item(i).getChildNodes()
                        .item(19).getTextContent()), Integer
                        .parseInt(episodes.item(i).getChildNodes()
                                .item(28).getTextContent()), aired, 0));

    }

Specifically here:

Integer.parseInt(episodes.item(i).getChildNodes().item(10).getTextContent())

And

Integer.parseInt(episodes.item(i).getChildNodes().item(19).getTextContent())

And you do it two more times (another Integer and a Long).You are attempting to parse empty strings ("") to an Integer. You should add a check before you parse:

    if (!aired.equals("") &&
        !episodes.item(i).getChildNodes().item(10).getTextContent().equals("") &&
        !episodes.item(i).getChildNodes().item(19).getTextContent().equals("")) {
        //code
    }

You may need to customize this check some more based on what your goals are. If it's empty, then don't continue the process. You can throw an error, write an error message, or continue.

Note : You should do more checks for empty strings and null pointers just in case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM