java - Take an xml feed from the internet and put it in my assets -
currently developping android application , i'm using xml feed take internet.the problem when phone doesn't have internet connection, application crashs. obtain xml file , put in assets.
my question : possible ? , if yes, what's best solution ?
check network connection:
public static boolean isconnectedtointernet(context context) { connectivitymanager connectivity = (connectivitymanager) context.getsystemservice(context.connectivity_service); if (connectivity != null) { networkinfo[] info = connectivity.getallnetworkinfo(); if (info != null) (int = 0; < info.length; i++) if (info[i].getstate() == networkinfo.state.connected) { return true; } } return false; }
download xml string
public string getxmlstring(string url) { try { url url1 = new url(url); urlconnection tc = url1.openconnection(); tc.setconnecttimeout(timeout); tc.setreadtimeout(timeout); bufferedreader br = new bufferedreader((new inputstreamreader(tc.getinputstream()))); stringbuilder sb = new stringbuilder(); string line; while ((line = br.readline()) != null) { sb.append(line + "\n"); } br.close(); return sb.tostring(); } catch (exception e) { log.d("error", "in xmldownloading"); } return null; }
save string directory in sd card:
private void writestringtotextfile(string s, string f) { file sdcard = environment.getexternalstoragedirectory(); file dir = new file(sdcard.getabsolutepath()); dir.mkdirs(); file file = new file(dir, f); try { fileoutputstream f1 = new fileoutputstream(file, false); printstream p = new printstream(f1); p.print(s); p.close(); f1.close(); } catch (filenotfoundexception e) { } catch (ioexception e) { } } }
Comments
Post a Comment