java - retriving stock quote with yahoo finance -
i have form button upon submit retrieve stock info yahoo finance api, , use display onto 3 textviews
the url use http://download.finance.yahoo.com/d/quotes.csv?s=goog&f=sl1p2
and button event handler is
url url; try { url = new url("http://download.finance.yahoo.com/d/quotes.csv?s=goog&f=sl1p2"); inputstream stream = url.openstream(); bufferedinputstream bis = new bufferedinputstream(stream); bytearraybuffer bab = new bytearraybuffer(50); int current = 0; while((current = bis.read()) != -1){ bab.append((byte) current); } string stocktxt = new string(bab.tobytearray()); string[] tokens = stocktxt.split(","); string stocksymbol = tokens[0]; string stockprice = tokens[1]; string stockchange = tokens[2]; string fstocksymbol = stocksymbol.substring(1, stocksymbol.length() -1); string fstockchange = stockchange.substring(1, stockchange.length()-3); symbolout.settext(fstocksymbol); priceout.settext(stockprice); changeout.settext(fstockchange); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
so noticed till first line there seems no problem i.e url = new url...,(even bare http request done through browser, return info need )
and manifest entry looks like
<uses-permission android:name="android.permission.internet"></uses-permission>
below logcat output
05-02 18:36:32.804: d/androidruntime(902): shutting down vm 05-02 18:36:32.804: w/dalvikvm(902): threadid=1: thread exiting uncaught exception (group=0x409c01f8) 05-02 18:36:33.113: e/androidruntime(902): fatal exception: main 05-02 18:36:33.113: e/androidruntime(902): android.os.networkonmainthreadexception 05-02 18:36:33.113: e/androidruntime(902): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1099) 05-02 18:36:33.113: e/androidruntime(902): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:391) 05-02 18:36:33.113: e/androidruntime(902): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:242) 05-02 18:36:33.113: e/androidruntime(902): @ java.net.inetaddress.getallbyname(inetaddress.java:220) 05-02 18:36:33.113: e/androidruntime(902): @ libcore.net.http.httpconnection.<init>(httpconnection.java:71)
so knows im going wrong
this key error in logcat message --> android.os.networkonmainthreadexception. should move network access background thread. can use either intentservice, asynchtask, handler, etc. accomplish this.
check out http://developer.android.com/training/articles/perf-anr.html more information why important network ops on separate thread.
Comments
Post a Comment