0

I wold like to save the given webservice url as xml file for offline sax parsing.

In Online i am using SAX Parser to parse the given url .

URL sourceUrl = new URL("http://www.example.com/mobile/eventinfo.php?id=20);    
InputSource inputSource = new InputSource(sourceUrl.openStream());
inputSource.setEncoding("ISO-8859-1");

For that I am getting the inputsourse from url as above and now I want to save that inputsourse as .xml file for offline parsing.

Here User has to load the application for very first time in online. And inside the application we are providing offline facilities. If user sets the app to offline the inputsourse has to save the xml file in local memory.

If the user starts the application in offline the application has to parse the data from saved xml file.

If the user is starts the application online the data has to parse from the url.

Can any one help me out here or share the knowledge

I am generating the xml file like the following code

try {
    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.connect();
    int responseCode = connection.getResponseCode();
    if(responseCode == 200){
        InputStream inputStream = connection.getInputStream();
        int size = connection.getContentLength();
        FileOutputStream fileOutputStream = new FileOutputStream(new File(getCacheDir(),filename));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String ch = null;
        while((ch = bufferedReader.readLine()) != null){
            fileOutputStream.write(ch.getBytes());
        }
    }else {

    }
}

but its raising an Exception as NetworkonMainThreadException at

  connection.connect();

thanks in advance.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Ganesh v
  • 29
  • 1
  • 5

1 Answers1

0

I think for the first time you generate the file (contains ur xml) in sdcard,and every time check if that file exists in sdcard then parse same otherwise load data from url and generate xml file.

Hope u get me what i say

Richa
  • 3,165
  • 1
  • 22
  • 26
  • Thank you. and how to generate the xml file with the given web service – Ganesh v Jul 06 '12 at 05:36
  • u need to create a file with .xml exntension and write ur content in that .xml file – Richa Jul 06 '12 at 05:43
  • try to get response using AsyncTask : refer http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Richa Jul 06 '12 at 07:22