androidhttpclient - android HttpURLConnection catch if nothing could download -


i'm using asynctask download xml given url. url preferencescreen. n

now i'm trying catch errors app doesn't crash gives nice toast error.

it works if user forgets 'http://' before url. when url formed there nothing download app still crashes , don't know how catch error: thought ioexception catch error doesn't.

@override protected void onpreexecute() {     progressdialog = new progressdialog(mcontext);     progressdialog.settitle("xml downloaden");     progressdialog.setmessage("downloading xml:");     progressdialog.setmax(100);     progressdialog.setprogressstyle(progressdialog.style_horizontal);     progressdialog.show(); }      @override protected void doinbackground(void... params) {      fileoutputstream fos = null;     inputstream inputstream = null;     httpurlconnection conn = null;     try {         fos = mcontext.openfileoutput("xmlapp.xml", context.mode_private);          sharedpreferences prefs = preferencemanager                 .getdefaultsharedpreferences(mcontext);         string start = prefs.getstring("pref_key_url",                 "http://sdesigns.be/eindwerk/web6/");          url url;         url = new url(start + "source/pages.xml");         conn = (httpurlconnection) url.openconnection();              byte[] buffer = new byte[64];             if (conn.getresponsecode() == httpurlconnection.http_ok) {                 int bytestodownload = conn.getcontentlength();                 int bytesdownloaded = 0;                 inputstream = conn.getinputstream();                 int len = 0;                 while ((len = inputstream.read(buffer)) > 0) {                     fos.write(buffer, 0, len);                     bytesdownloaded += len;                     publishprogress((float) bytesdownloaded / bytestodownload);                 }                 inputstream.close();                 fos.close();             }         } catch (malformedurlexception e) {         this.e = e;     } catch (ioexception e) {         this.e = e;     }      {          conn.disconnect();     }      return null; }  @override protected void onpostexecute(void result) {      if (progressdialog != null) {         progressdialog.dismiss();         progressdialog = null;     }     if (e == null) {         list<string> images = new xmlparsersax(mcontext).getimageslist();         downloadimages di = new downloadimages(mcontext);         di.execute(images);     } else {         toast.maketext(mcontext,                 "check url!" + e.tostring(),                 toast.length_long).show(); //show error     }  } 

edit

stacktrace:

    04-19 10:18:40.904: e/windowmanager(2425): activity com.example.eindwerkappv1.preferences has leaked window com.android.internal.policy.impl.phonewindow$decorview@41a97848 added here 04-19 10:18:40.904: e/windowmanager(2425): android.view.windowleaked: activity com.example.eindwerkappv1.preferences has leaked window com.android.internal.policy.impl.phonewindow$decorview@41a97848 added here 04-19 10:18:40.904: e/windowmanager(2425):  @ android.view.viewrootimpl.<init>(viewrootimpl.java:344) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:267) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:215) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.view.windowmanagerimpl$compatmodewrapper.addview(windowmanagerimpl.java:140) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.view.window$localwindowmanager.addview(window.java:537) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.app.dialog.show(dialog.java:278) 04-19 10:18:40.904: e/windowmanager(2425):  @ com.example.eindwerkappv1.downloadxml.onpreexecute(downloadxml.java:38) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.os.asynctask.executeonexecutor(asynctask.java:561) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.os.asynctask.execute(asynctask.java:511) 04-19 10:18:40.904: e/windowmanager(2425):  @ com.example.eindwerkappv1.preferences$1.onpreferenceclick(preferences.java:48) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.preference.preference.performclick(preference.java:941) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.preference.preferencescreen.onitemclick(preferencescreen.java:202) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.widget.adapterview.performitemclick(adapterview.java:292) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.widget.abslistview.performitemclick(abslistview.java:1068) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.widget.abslistview$performclick.run(abslistview.java:2525) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.widget.abslistview$1.run(abslistview.java:3186) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.os.handler.handlecallback(handler.java:605) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.os.handler.dispatchmessage(handler.java:92) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.os.looper.loop(looper.java:137) 04-19 10:18:40.904: e/windowmanager(2425):  @ android.app.activitythread.main(activitythread.java:4441) 04-19 10:18:40.904: e/windowmanager(2425):  @ java.lang.reflect.method.invokenative(native method) 04-19 10:18:40.904: e/windowmanager(2425):  @ java.lang.reflect.method.invoke(method.java:511) 04-19 10:18:40.904: e/windowmanager(2425):  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:823) 04-19 10:18:40.904: e/windowmanager(2425):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:590) 04-19 10:18:40.904: e/windowmanager(2425):  @ dalvik.system.nativestart.main(native method) 

you may want account possibility of socket timing out using conn.setreadtimeout(10000); before open connection, add catch clause handle sockettimeoutexception. note choice of 10000ms timeout arbitrary.

one main problem you're not handling happens when server returns response other http_ok. if (conn.getresponsecode() == httpurlconnection.http_ok) false, there no exceptions caught. malformedurlexception thrown formatting errors in url, , ioexception in situations try read closed socket, or similar. there nothing catch http_not_found. quick fix guess adding else clause if(conn.getresponsecode() == httpurlconnection.http_ok).

else {     e = new exception(); } 

this way, e not null in onpostexecute(). of course not practice, work code.

edit: following code worked me fine. had fill in few areas didn't include.

private class downloadtask extends asynctask<void, float, void> {     context mcontext;     progressdialog progressdialog;     exception e;      downloadtask(context context) {         mcontext = context;     }      @override     protected void onpreexecute() {         progressdialog = new progressdialog(mcontext);         progressdialog.settitle("xml downloaden");         progressdialog.setmessage("downloading xml:");         progressdialog.setmax(100);         progressdialog.setprogressstyle(progressdialog.style_horizontal);         progressdialog.show();     }      @override     protected void onprogressupdate(float... values) {         for( float val : values) {             progressdialog.setprogress((int)(val*100f));         }     }      @override     protected void doinbackground(void... params) {          fileoutputstream fos = null;         inputstream inputstream = null;         httpurlconnection conn = null;         try {             fos = mcontext.openfileoutput("xmlapp.xml", context.mode_private);              sharedpreferences prefs = preferencemanager                     .getdefaultsharedpreferences(mcontext);             string start = prefs.getstring("pref_key_url",                     "http://sdesigns.be/eindwerk/web6/");              url url;             url = new url(start);             conn = (httpurlconnection) url.openconnection();             conn.setdoinput(true);             conn.setrequestmethod("get");             conn.setreadtimeout(50000);             conn.connect();                  byte[] buffer = new byte[64];                 if (conn.getresponsecode() == httpurlconnection.http_ok) {                     int bytestodownload = conn.getcontentlength();                     int bytesdownloaded = 0;                     inputstream = conn.getinputstream();                     int len = 0;                     while ((len = inputstream.read(buffer)) > 0) {                         fos.write(buffer, 0, len);                         bytesdownloaded += len;                         publishprogress((float) bytesdownloaded / bytestodownload);                     }                     inputstream.close();                     fos.close();                 } else {                     e = new exception("url not found.");                 }          } catch (exception e) {             //since don't differentiate exceptions , check if e null, might have 1 catch block             this.e = e;         }         {             try {              conn.disconnect();             } catch (exception e) {}             conn = null;         }          return null;     }      @override     protected void onpostexecute(void result) {          if (progressdialog != null) {             progressdialog.dismiss();             progressdialog = null;         }         if (e == null) {             toast.maketext(mcontext,                     "success!",                     toast.length_long).show();             list<string> images = new xmlparsersax(mcontext).getimageslist();             downloadimages di = new downloadimages(mcontext);             di.execute(images);         } else {             toast.maketext(mcontext,                     "check url!" + e.tostring(),                     toast.length_long).show(); //show error         }      } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -