android AsyncTask xml parsing

后端 未结 1 1378
不知归路
不知归路 2021-01-17 06:57

i try to parse a large xml document in a AsyncTask. The FeinstaubActivity starts but i only see a black screen and then get back to the RSSReaderActivity from where i starte

相关标签:
1条回答
  • 2021-01-17 07:06

    Ah, not sure you can do that: http://developer.android.com/reference/android/os/AsyncTask.html shows that execute returns void.

    You want this:

    Document document = null;
    
        protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.feinstaub);
           new ParseXMLFile().execute();
        }
    
        void setDocument(Document document){ this.document = document; }
    
        private class ParseXMLFile extends AsyncTask<Integer, Integer, Document>{
        @Override public void onPostExecute(Document d){ setDocument(d); }
    
        @Override
        protected Document doInBackground(Integer... params) {
            Document parsedXML = null;
            try {
                parsedXML = builder.parse(getApplicationContext().getResources().openRawResource(R.raw.finedust));
            } catch (NotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return parsedXML;
        }
    
    }
    

    There's a bit of scoping you need to do: pass the parent activity in or declare the setDocument method appropriately somehow so you can call it back from inside the AsyncTask, but something like that should do it.

    0 讨论(0)
提交回复
热议问题