android - Exiting asynctask midway through doinBackground on conditional -
i'm loading data async task. in doinbackground()
, want check if json string returned contains error, if want stop asynctask , display textview, if there's no error, want continue executing asynctask. code.
protected string doinbackground(string... args) { // building parameters httpclient client = new defaulthttpclient(); httppost post = new httppost(url_all_open_bets); list<namevaluepair> params = new arraylist<namevaluepair>(); post.setheader("user-agent","mozilla/5.0 (macintosh; intel mac os x 10_9_5) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.81 safari/537.36"); params.add(new basicnamevaluepair("email", name)); params.add(new basicnamevaluepair("user-agent","mozilla/5.0 (macintosh; intel mac os x 10_9_5) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.81 safari/537.36")); try { post.setentity(new urlencodedformentity(params)); } catch (ioexception ioe) { ioe.printstacktrace(); } try { httpresponse response = client.execute(post); log.d("http post response:", response.tostring()); httpentity httpentity = response.getentity(); inputstream = httpentity.getcontent(); jsonobject jobj = null; string json = ""; try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { if (!line.startswith("<", 0)) { if (!line.startswith("(", 0)) { sb.append(line + "\n"); } } } is.close(); json = sb.tostring(); json = json.substring(json.indexof('{')); if (json.contains("error")) { textview textview = (textview) findviewbyid(r.id.nobetstxtbox); textview.setvisibility(view.visible); } log.d("sb", json); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string log.d("json", jobj.tostring()); try { allgames = jobj.getjsonarray(tag_bet); log.d("allgames", allgames.tostring()); arraylist<betdatabasesaver> listofbets = new arraylist<>(); // looping through products (int = 0; < allgames.length(); i++) { jsonobject c = allgames.getjsonobject(i); // storing each json item in variable string id = c.getstring(tag_id); string user = c.getstring(tag_user); string returns = c.getstring(tag_returns); string stake = c.getstring(tag_stake); string status = c.getstring(tag_status); string teams = c.getstring(tag_teams); log.d("id", id); log.d("user", user); log.d("returns", returns); log.d("stake", stake); log.d("status", status); log.d("teams", teams); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_id, id); map.put(tag_teams, teams); map.put(tag_user, user); map.put(tag_returns, returns); map.put(tag_stake, stake); map.put(tag_status, status); useroutcomes.put(id.substring(0, 10), teams); boolean contains = false; (int = 0; < listwriter.size(); a++) { if (listwriter.get(a).getid().equals(id)) { listwriter.add(a, new betdisplayer(user, id, integer.parseint(stake), integer.parseint(returns), status, "", "", teams)); contains = true; } } if (!(contains)) { listwriter.add(i, new betdisplayer(user, id, integer.parseint(stake), integer.parseint(returns), status, "", "", teams)); } // adding hashlist arraylist bet.add(map); } } catch (jsonexception e) { e.printstacktrace(); } } catch (ioexception ioe) { ioe.printstacktrace(); } return ""; }
i added fragment
` if (json.contains("error")) { textview textview = (textview) findviewbyid(r.id.nobetstxtbox); textview.setvisibility(view.visible); }`
to check error, i'm not sure how exit asynctask @ point if error true , not carry out of code after statement. using else won't work interfere try catch statement, using break doesn't work.
you can call cancel()
method in if condition, calling cancel()
in doinbackground()
cancel asynctask
, oncancelled()
called instead of onpostexecute()
like this:
if (json.contains("error")) { cancel(true); return ""; }
Comments
Post a Comment