【发布时间】:2013-02-11 02:50:49
【问题描述】:
我使用SAX 到parse 一个XML File 在线存储。然后填充自定义GridView
这工作正常。
现在我想将应用程序开始处的 XML 文件下载到local 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 Data handling and XML version control concept
最后,我想将URL 或FilePath 传递给AsyncTask
【问题讨论】:
标签: java android xml-parsing local-storage sax