java - Handle Exception with error dialog in asynctask -
i making rss android app pull reload. want use asynctask. here code:
class refresh extends asynctask<string,void,void>{ @override protected void doinbackground(string... params) { try { download(getresources().getstring(r.string.link)); }catch (exception e){ createneterrordialog(); } return null; } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); try{ views(); }catch (exception e){ toast.maketext(mainactivity.this, "error", toast.length_long).show(); cancel(true); } lstpost.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(mainactivity.this, webviewactivity.class); bundle bundle = new bundle(); document xmlfeed = rssfeed .getrssfromserver(getresources().getstring(r.string.link)); nodelist nodes = xmlfeed.getelementsbytagname(getresources().getstring(r.string.item)); element item = (element) nodes.item(position); string summary = rssfeed.getvalue(item, key_summary); string title = rssfeed.getvalue(item, key_title); bundle.putstring(getresources().getstring(r.string.tag_summary), summary); bundle.putstring(getresources().getstring(r.string.tag_title), title); intent.putextras(bundle); startactivity(intent); } }); } } void download(string url) throws exception{ lists.clear(); document xmlfeed = rssfeed.getrssfromserver(url); nodelist nodes = xmlfeed.getelementsbytagname("entry"); (int = 0; < nodes.getlength(); i++) { element item = (element) nodes.item(i); hashmap<string, object> feed = new hashmap<string, object>(); feed.put(key_title, rssfeed.getvalue(item, key_title)); feed.put(key_summary, rssfeed.getvalue(item, key_summary)); feed.put(key_link, rssfeed.getvalue(item, key_link)); //feed.put(key_date, rssfeed.getvalue(item, key_date)); post_lists.add(feed); lists.add(feed.get(key_title).tostring()); } } void views(){ lstpost = (listview) findviewbyid(r.id.lstposts); adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_2, android.r.id.text1, lists) { @override public view getview(int position, view convertview, viewgroup parent) { view view = super.getview(position, convertview, parent); textview txt1 = (textview) view .findviewbyid(android.r.id.text1); textview txt2 = (textview) view .findviewbyid(android.r.id.text2); hashmap<string, object> data = post_lists.get(position); txt1.settext(html.fromhtml(data.get(key_title).tostring())); if (data.get(key_summary).tostring().length() > 125) { txt2.settext(html.fromhtml((data.get(key_summary).tostring()).substring(0, 125) + "...")); } else { txt2.settext(html.fromhtml(data.get(key_summary).tostring())); } return view; } }; textview dateview=(textview) findviewbyid(r.id.date); calendar rightnow = calendar.getinstance(); dateformat formatter = new simpledateformat(getresources().getstring(r.string.date_format)); dateview.settext(getresources().getstring(r.string.lastupdate) + formatter.format(rightnow.gettime())); lstpost.setadapter(adapter); } protected void createneterrordialog() { alertdialog.builder builder = new alertdialog.builder(this); builder.setmessage(getresources().getstring(r.string.errorbox)) .settitle("unable connect") .setcancelable(false) .setpositivebutton("settings", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { intent = new intent(settings.action_wireless_settings); startactivity(i); } } ) .setneutralbutton("refresh", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { new refresh().execute(getresources().getstring(r.string.link)); } }) .setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { finish(); } } ); alertdialog alert = builder.create(); alert.show(); }
problem: if switch network connection off, app exits instead of displaying createneterrordialog(). can please me out on here?
use below method check whether net on or off:
public static boolean isdeviceonline(context context) { connectivitymanager connmgr = (connectivitymanager) context.getsystemservice(context.connectivity_service); networkinfo networkinfo = connmgr.getactivenetworkinfo(); boolean isonline = (networkinfo != null && networkinfo.isconnected()); if(!isonline) toast.maketext(context, " no internet connection ", toast.length_short).show(); return isonline; }
and when need tto check if device online call it:
if(isdeviceonline(context)) { //call refresh } else { //whatever want }
i thing exception handling not practice it.
Comments
Post a Comment