0

I would be glad if you help me. I have an app. When I open the app, writing XML file to "sdcard/example.xml". (with internet connection) But when I open the app without internet connection, the app does not read XML file. Here is my code;

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) {
    }
}

And the xmlDownload function is

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();
        }
    }
}

and xmlRead function is;

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();
    }
}

How can i (ONLY) read xml file without internet?

2
  • I'm voting to close this question as off-topic because the question has been edited away rather than deleted.
    – Tim
    Jun 29, 2015 at 13:06
  • I Solved this problem another way. Jun 29, 2015 at 13:09

2 Answers 2

1

Your approach is logically incorrect.Try implementing this logical procedure.

  1. Check if there is an internet connection available or not.
  2. If there is a internet connection download , put in SD card and read XML.
  3. If there is no internet connection, check if the xml is available in SD card or not,if available read xml file.

in your case if there is no internet connection "xmlDownload" will throw exception and will not execute "xmlRead" funciton.

0

It is because when there is no connection, your code :

        try {
        File xmlWriteSd = new File("/sdcard/example.xml");
        FileOutputStream fos = new FileOutputStream(xmlWriteSd);
        xmlDownload(fos,"http://someExample.com/rss/example.xml");
        xmlRead();

Catches an exception on xmlDownload(..); and does not perform the xmlRead(); you can solve this in many ways, I suggest you just put your xmlRead() on a finally and remove the first return like this :

   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;
    }
    }

Again this is not the best way to do it, the best way would be to implement a test on connectivity before doing any download or read and see what you want to do based on the result.

Not the answer you're looking for? Browse other questions tagged or ask your own question.