java - Cancel an operation on Toast -
i wrote jsoup html scraping class android project. custom class puts user-inputted zip code constructor, , parse html. works in asynchronous thread main thread. since there no right way deal incorrect zip codes, had check null
in particular element, specifically:
if(doc.select("div.columns").first().text() == null) { ((activity) context).runonuithread(new runnable() { public void run() { toast toast = toast.maketext(context, r.string.toast_parse_fail, toast.length_long); toast.show(); return; } }); }
if particular element null
(meaning no such data exists zip code), create toast user. why try
wouldn't work in case, because jsoup , java doesn't know whether parse failed or not, since web page still loads fine; only, there no data, crash app nullpointerexception
code following it.
despite toast
exception handling using .runonuithread
, app still crash. there particular methods cancel operations follow null
-checking method? know crashing because toast
not cancelling operations, , preceding execute following code causes nullpointerexceptions
.
posted full pollen
constructor.
public pollen(int zipcode, final context context) { this.context = context; this.zipcode = zipcode; document doc; try { // pass address wunderground's website using our inputted zipcode doc = jsoup.connect("http://www.wunderground.com/displaypollen.asp?zipcode=" + this.zipcode).get(); if(doc.select("div.columns").first().text() == null) { ((activity) context).runonuithread(new runnable() { public void run() { toast toast = toast.maketext(context, r.string.toast_parse_fail, toast.length_long); toast.show(); return; } }); } // "location" xml element location = doc.select("div.columns").first(); this.location = location.text(); // "pollen type" xml element pollentype = doc.select("div.panel h3").first(); this.pollentype = pollentype.text(); simpledateformat format = new simpledateformat("eee mmmm dd, yyyy"); // add 4 items of pollen , dates // respective list for(int = 0; < 4; i++) { element dates = doc.select("td.text-center.even-four").get(i); element levels = doc.select("td.levels").get(i); try { pollenmap.put(format.parse(dates.text()), levels.text()); } catch (parseexception e) { ((activity) context).runonuithread(new runnable() { public void run() { toast toast = toast.maketext(context, r.string.toast_parse_fail, toast.length_long); toast.show(); return; } }); } } } catch (ioexception e) { ((activity) context).runonuithread(new runnable() { public void run() { toast toast = toast.maketext(context, r.string.toast_parse_fail, toast.length_long); toast.show(); return; } }); } }
tl;dr still want toast
show - want on-toast, cancel operations follow it, since know if particular toast
shows, if code following executes, crash app.
since in try-catch
-block, can't throw exception?
public pollen(int zipcode, final context context) { this.context = context; this.zipcode = zipcode; document doc; try { // pass address wunderground's website using our inputted zipcode doc = jsoup.connect("http://www.wunderground.com/displaypollen.asp?zipcode=" + this.zipcode).get(); if(doc.select("div.columns").first().text() == null) { // oh no! div.colums empty. lets throw exception, // prevent code below executing. throw new illegalstateexception("div.columns null"); } // "location" xml element location = doc.select("div.columns").first(); this.location = location.text(); // "pollen type" xml element pollentype = doc.select("div.panel h3").first(); this.pollentype = pollentype.text(); simpledateformat format = new simpledateformat("eee mmmm dd, yyyy"); // add 4 items of pollen , dates // respective list for(int = 0; < 4; i++) { element dates = doc.select("td.text-center.even-four").get(i); element levels = doc.select("td.levels").get(i); // removed nested try-catch block pollenmap.put(format.parse(dates.text()), levels.text()); } } catch (ioexception e) { displaytoast(context); } catch (parseexception e) { // catch parseexception here instead of nesting try-catch blocks. displaytoast(context); } catch (illegalstateexception e) { // catch illegalstateexception thrown when div.columns null, // , let user know went wrong. displaytoast(context); } } private void displaytoast(context context) { ((activity) context).runonuithread(new runnable() { public void run() { toast toast = toast.maketext(context, r.string.toast_parse_fail, toast.length_long); toast.show(); } }); }
Comments
Post a Comment