如果你能帮助我,我会很高兴的。我有一个应用程序。当我打开应用程序时,将XML文件写入"sdcard/example.XML"。(带互联网连接)但当我在没有互联网连接的情况下打开应用程序时,该应用程序不会读取XML文件。这是我的密码;
class RetrieveFeedTask extends AsyncTask<String, Void, String> {
private Exception exception;
protected String doInBackground(String... urls) {
try {
File xmlWriteSd = new File("/sdcard/example.xml");
FileOutputStream fos = new FileOutputStream(xmlWriteSd);
xmlDownload(fos,"http://someExample.com/rss/example.xml");
xmlRead();
return null;
} catch (Exception e) {
this.exception = e;
return null;
}
}
protected void onPostExecute(String feed) {
}
}
xmlDownload函数是
public void xmlDownload(FileOutputStream fout, final String urlString) throws MalformedURLException, IOException {
BufferedInputStream in =null;
File file = new File("/sdcard/example.xml");
if(!file.exists())//control
return;
try {
in = new BufferedInputStream(new URL(urlString).openStream());
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} finally {
if (in != null) {
in.close();
}
if (fout != null) {
fout.close();
}
}
}
xmlRead函数为;
private void xmlRead() {
try {
File fXmlFile = new File("/sdcard/example.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("item");
for (int temp = 0; temp < nList.getLength(); temp++) {
final ExampleObject = new YemekNesne();
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
ExampleObject .setDate(eElement.getElementsByTagName("Date").item(0).getTextContent());
ExampleObject .setmain(eElement.getElementsByTagName("main").item(0).getTextContent());
ExampleObject .setsecond(eElement.getElementsByTagName("second").item(0).getTextContent());
ExampleObject .setextra(eElement.getElementsByTagName("extra").item(0).getTextContent());
ExampleObject .setExample(eElement.getElementsByTagName("Example").item(0).getTextContent());
listOfExample.put(ExampleObject .getDate(),ExampleObject );//hash map
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
在没有互联网的情况下,我如何(仅)读取xml文件?
您的方法在逻辑上是不正确的。尝试实现此逻辑过程。
- 检查是否有可用的互联网连接
- 如果有互联网连接下载,请放入SD卡并读取XML
- 如果没有互联网连接,请检查SD卡中的xml是否可用,如果可用,请读取xml文件
在您的情况下,如果没有互联网连接,"xmlDownload"将抛出异常,并且不会执行"xmlRead"功能。
这是因为当没有连接时,您的代码:
try {
File xmlWriteSd = new File("/sdcard/example.xml");
FileOutputStream fos = new FileOutputStream(xmlWriteSd);
xmlDownload(fos,"http://someExample.com/rss/example.xml");
xmlRead();
在xmlDownload(..)上捕获一个异常;并且不执行xmlRead();你可以用很多方法来解决这个问题,我建议你把xmlRead()放在finally上,然后删除第一个返回,如下所示:
try {
File xmlWriteSd = new File("/sdcard/example.xml");
FileOutputStream fos = new FileOutputStream(xmlWriteSd);
xmlDownload(fos,"http://someExample.com/rss/example.xml");
return null;
} catch (Exception e) {
this.exception = e;
} finally {
try{
xmlRead();
} catch (Exception e) {
this.exception = e;
return null;
}
}
同样,这不是最好的方法,最好的方法是在进行任何下载或阅读之前对连接进行测试,并根据结果查看您想做什么。