java - Android:Not sure why my method isnt returning ArrayList<HashMap<String, String>> -
hey problem in asynctask method doinbackground have defined return type of arraylist<hashmap<string, string>>
, in code return menuitems type arraylist<hashmap<string, string>>
. have seen similar situation said not exits support return arraylist<hashmap<string, string>>
looking @ code cant see return other arraylist<hashmap<string, string>>
apppreciated. here code doinbackground
protected arraylist<hashmap<string, string>> doinbackground(string...petrolpriceurl){ arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); { for(int = 0; < 100; i++){ publishprogress(1); try { thread.sleep(100); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } string urlstring = petrolpriceurl.tostring(); string result = ""; inputstream aninstream = null; int response = -1; url url = null; try { url = new url(urlstring); } catch (malformedurlexception e) { // todo auto-generated catch block return null; } urlconnection conn = null; try { conn = url.openconnection(); } catch (ioexception e) { // todo auto-generated catch block return null; } // check connection can opened if (!(conn instanceof httpurlconnection)) try { throw new ioexception("not http connection"); } catch (ioexception e) { // todo auto-generated catch block return null; } try { // open connection httpurlconnection httpconn = (httpurlconnection) conn; httpconn.setallowuserinteraction(false); httpconn.setinstancefollowredirects(true); httpconn.setrequestmethod("get"); httpconn.connect(); response = httpconn.getresponsecode(); // check connection ok if (response == httpurlconnection.http_ok) { // connection ok open reader aninstream = httpconn.getinputstream(); inputstreamreader in= new inputstreamreader(aninstream); bufferedreader bin= new bufferedreader(in); // read in data rss stream string line = new string(); while (( (line = bin.readline())) != null) { result = result + "\n" + line; log.v(tag, "index=" + result); } } } catch (ioexception ex) { try { throw new ioexception("error connecting"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } handler parser = new handler(); string xml = result.tostring(); // getting xml document doc = parser.getdomelement(xml); // getting dom element nodelist nl = doc.getelementsbytagname(key_fuel); // looping through item nodes <item> (int = 0; < nl.getlength(); i++) { // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); element e = (element) nl.item(i); // adding each child node hashmap key => value map.put(key_highest, parser.getvalue(e, key_highest)); map.put(key_average, parser.getvalue(e, key_average)); map.put(key_lowest, "rs." + parser.getvalue(e, key_lowest)); map.put(key_link, parser.getvalue(e, key_link)); // adding hashlist arraylist menuitems.add(map); } return menuitems; } } }
the error getting
this method must return result of type arraylist<hashmap<string,string>> petrolpriceactivity.java /petrolpricetestproject/src/org/me/myandroidstuff line 78
reevaluated code:
@override protected arraylist<hashmap<string, string>> doinbackground(string...petrolpriceurl) { for(int = 0; < 100; i++) { publishprogress(1); try { thread.sleep(100); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } string urlstring = petrolpriceurl.tostring(); string result = ""; inputstream aninstream = null; int response = -1; url url = null; try { url = new url(urlstring); } catch (malformedurlexception e) { // todo auto-generated catch block return null; } urlconnection conn = null; try { conn = url.openconnection(); } catch (ioexception e) { // todo auto-generated catch block return null; } // check connection can opened if (!(conn instanceof httpurlconnection)) { try { throw new ioexception("not http connection"); } catch (ioexception e) { // todo auto-generated catch block return null; } } try { // open connection httpurlconnection httpconn = (httpurlconnection) conn; httpconn.setallowuserinteraction(false); httpconn.setinstancefollowredirects(true); httpconn.setrequestmethod("get"); httpconn.connect(); response = httpconn.getresponsecode(); // check connection ok if (response == httpurlconnection.http_ok) { // connection ok open reader aninstream = httpconn.getinputstream(); inputstreamreader in= new inputstreamreader(aninstream); bufferedreader bin= new bufferedreader(in); // read in data rss stream string line = new string(); while (( (line = bin.readline())) != null) { result = result + "\n" + line; log.v(tag, "index=" + result); arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); handler parser = new handler(); string xml = result.tostring(); // getting xml document doc = parser.getdomelement(xml); // getting dom element nodelist nl = doc.getelementsbytagname(key_fuel); // looping through item nodes <item> (int = 0; < nl.getlength(); i++) { // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); element e = (element) nl.item(i); // adding each child node hashmap key => value map.put(key_highest, parser.getvalue(e, key_highest)); map.put(key_average, parser.getvalue(e, key_average)); map.put(key_lowest, "rs." + parser.getvalue(e, key_lowest)); map.put(key_link, parser.getvalue(e, key_link)); // adding hashlist arraylist menuitems.add(map); return menuitems; } } } } catch (ioexception ex) { try { throw new ioexception("error connecting"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } //return menuitems; }
you don't return in try {}
block :
try { // open connection httpurlconnection httpconn = (httpurlconnection) conn; httpconn.setallowuserinteraction(false); httpconn.setinstancefollowredirects(true); httpconn.setrequestmethod("get"); httpconn.connect(); response = httpconn.getresponsecode(); // check connection ok if (response == httpurlconnection.http_ok) { // connection ok open reader aninstream = httpconn.getinputstream(); inputstreamreader in= new inputstreamreader(aninstream); bufferedreader bin= new bufferedreader(in); // read in data rss stream string line = new string(); while (( (line = bin.readline())) != null) { result = result + "\n" + line; log.v(tag, "index=" + result); } } }
which means if no ioexception
raised, nothing returned.
seems got logic in catch (ioexception ex)
clause may indicate should refactor code (try getting whole menuitems
thing out of catch clause).
edit following reevaluated code :
let's take @ last try-catch blocks end of method :
try { // open connection httpurlconnection httpconn = (httpurlconnection) conn; // remainder omitted } catch (ioexception ex) { try { throw new ioexception("error connecting"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } //return menuitems;
in try block return menuitems
(of type arraylist<hashmap<string, string>>
) within loop end loop iterating once before returning menuitems
object caller, ending method call (and since it's not want, should menuitems
out of loop).
then think happen if ioexception thrown before menuitems
object returned (can't happen after) ? exception caught in catch clause throw exception catch after print current stack trace , then... nothing. function end without returning compiler complains about.
so should find way menuitems
returned no matter (hint : since java variables block-scoped, getting menuitems
declaration out of loop might not enough. on track last code line !)
Comments
Post a Comment