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.