Connection to web server and reading XML files(Android) -
what want connect web server , can read xml file exception open failed erofs (read-only file system). added in manifest:
uses-permission android:name="android.permission.read_external_storage" what doing wrong?
public void gethtml() throws ioexception { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); try { url url = new url("http:/..."); httpurlconnection conn = (httpurlconnection) url.openconnection(); bufferedreader rd; string line; string name = "myfile.xml"; conn.setrequestmethod("get"); rd = new bufferedreader(new inputstreamreader(conn.getinputstream())); file file = new file(name); if(file.exists()) { file.delete(); file = new file(name); } filewriter fw = new filewriter(file.getname(), true); bufferedwriter bw = new bufferedwriter(fw); printwriter out = new printwriter(bw); while ((line = rd.readline()) != null) { system.out.println(line); fw.write(line); fw.write("\n"); //out.println(line); //out.flush(); } fw.close(); out.close(); rd.close(); } catch(exception e){ toast.maketext(getapplicationcontext()," "+e,toast.length_short).show(); } } }
i made changes , solved.i'll post correct code.
public void gethtml() throws ioexception {
fileoutputstream outputstream=null; strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); string fullpath = "/mnt/sdcard/"; try { url url = new url("http:.."); httpurlconnection conn = (httpurlconnection) url.openconnection(); bufferedreader rd; file dir = new file(fullpath.tostring()); if (!dir.exists()) { dir.mkdirs(); } file file = new file(fullpath, "file.xml"); if(file.exists()) file.delete(); file.createnewfile(); string line; outputstream = new fileoutputstream(file); conn.setrequestmethod("get"); rd = new bufferedreader(new inputstreamreader(conn.getinputstream())); string newline="\n"; while ((line = rd.readline()) != null){ outputstream.write(line.getbytes()); outputstream.write(newline.getbytes()); outputstream.flush(); } outputstream.close(); rd.close(); } catch(exception e){ toast.maketext(getapplicationcontext()," "+e,toast.length_short).show(); } }
Comments
Post a Comment