android - How to create, write and read XML file in internal storage -
i developing android app, accesses xml file saved on remote location when device finds net connectivity. want save file locally in app, if device doesn't have info connectivity, app can parse info local xml file. next code i'm using
private class listasync extends asynctask<void, void, void> { file path = new file(getfilesdir(),"my_data_folder"); @override protected void onpreexecute() { log.i("test", "running on ui thread"); } @override protected void doinbackground(void... params) { nameaddresslist = null; final xmldomparserhandler parser = new xmldomparserhandler( tag_name.tolowercase(), listofplacesactivity.this); seek { checkconnectivity checkconnectivity = new checkconnectivity(listofplacesactivity.this); if (checkconnectivity.isconnectivityon()) { url myurl = new url("http://somewebsite.com/data.xml"); urlconnection ucon = myurl.openconnection(); final inputstream = ucon.getinputstream(); seek { if( !path.isdirectory()) { path.mkdir(); } file datafile = new file(path,"data.xml"); fileoutputstream fos = new fileoutputstream(datafile); string info = getstringfrominputstream(is); fos.write(data.getbytes()); fos.close(); } grab (ioexception e) { e.printstacktrace(); } nameaddresslist = parser.parsexml(is); } else { seek { file datafile = new file(path,"data.xml"); fileinputstream fis = new fileinputstream(datafile); datainputstream in = new datainputstream(fis); nameaddresslist = parser.parsexml(in); in.close(); } grab (ioexception e) { e.printstacktrace(); } nameaddresslist = parser.parsexml(getinputstream("data.xml")); } } grab (malformedurlexception e) { e.printstacktrace(); } grab (connectexception e){ e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } homecoming null; } @override protected void onpostexecute(void result) { listofplacesadapter listofplacesadapter = new listofplacesadapter(listofplacesactivity.this, nameaddresslist); listview.setadapter(listofplacesadapter); listview.setitemscanfocus(true); spinner.dismiss(); } private inputstream getinputstream(string filename){ inputstream inputstream = null; seek { inputstream = getassets().open(filename); } grab (ioexception e) { e.printstacktrace(); } homecoming inputstream; } private string getstringfrominputstream(inputstream is) { bufferedreader br = null; stringbuilder sb = new stringbuilder(); string line; seek { br = new bufferedreader(new inputstreamreader(is)); while ((line = br.readline()) != null) { sb.append(line); } } grab (ioexception e) { e.printstacktrace(); } { if (br != null) { seek { br.close(); } grab (ioexception e) { e.printstacktrace(); } } } homecoming sb.tostring(); } }
above code not working not giving error well. please tell going wrong.
you may consider using android sync adapter , periodic/manual sync of xml files server local storage , vise versa. android take care of sync when network connection available. here link tutorial
http://developer.android.com/training/sync-adapters/index.html
best approach utilize local xml file updates , sync server after update , making sure both client , server has same version. can phone call sync adapter manually after update local file , trigger immediate sync server. during sync, if there exception (eg:- network not available) set sync retry setting right flag. sync adapter take care of retry when network available again. when server info changes user/application, send gcm broadcast message application , utilize broadcast message trigger sync 1 time again local re-create gets latest version server. can utilize timestamps decide 1 latest
android xml
No comments:
Post a Comment