lundi 29 juin 2015

How to read a xml file in android without internet connection?

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://ift.tt/1BQfs81");
            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?

Aucun commentaire:

Enregistrer un commentaire