0

我正在使用在线SAX存储。然后填充一个自定义 这工作正常。现在我想在应用程序开始处下载 XML 文件,然后再解析它。parseXML FileGridViewlocal storage

解析:

class loadingTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            gridView.setAdapter(new LkwAdapter(Lkw.this,itemList));
            showProgress.dismiss();
        }

        @Override
        protected String doInBackground(String... strings) {

            try{
                URL contURL = new URL(rUrl);
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();
                LkwParseHandler handler = new LkwParseHandler();
                xr.setContentHandler(handler);
                xr.parse(new InputSource(contURL.openStream()));//THIS WORKS
                //xr.parse(new InputSource(new FileInputStream("test.xml")));
                //THIS IS ONE OF MY MANY TRIES, what InputSource do i use?

                itemList = handler.getLkwItems();

            }catch (Exception e){
                e.printStackTrace();
            }
            return "";
        }
    }

这适用于在线文件!然后当我第一次保存文件并尝试使用它时,GridView 保持为空!

保存文件:(在初始屏幕上的线程中)

InputStream input = null;
FileOutputStream output = null;
try {
   URL url = new URL("http://test.com/test.xml");
   input = url.openConnection().getInputStream();
   output = openFileOutput("test.xml", Context.MODE_PRIVATE);

   int read;
   byte[] data = new byte[1024];
   while ((read = input.read(data)) != -1)
       output.write(data, 0, read);

   output.close();
   input.close();

} catch (MalformedURLException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
} finally {
    Intent i = new Intent(SplashScreen.this, MainMenu.class);
   }

   startActivity(i);
   finish();
}

我首先保存正确吗?我错过了什么?

在这个问题中,您可以看到我正在尝试做什么Android 数据处理和 XML 版本控制概念 最后我想将 aURL或 a传递FilePathAsyncTask

4

1 回答 1

0

由于甚至没有人看过这个问题(这里有什么交易??)我在几个小时后使用:

...
FileInputStream fis =  openFileInput("test.xml");
InputStreamReader in = new InputStreamReader(fis);

xr.parse(new InputSource(in));
...
于 2013-02-26T11:59:29.667 回答