简体   繁体   中英

retriving stock quote with yahoo finance

i have this form with a button upon submit i retrieve stock info from yahoo finance api, and use it to display onto three textviews

the url i use http://download.finance.yahoo.com/d/quotes.csv?s=goog&f=sl1p2

and my 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 what i noticed till the first line there seems to be no problem ie url = new URL...,(even the bare http request done through browser, does return the info i need )

and my manifest entry looks like

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

below is the 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 with 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): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 05-02 18:36:33.113: E/AndroidRuntime(902): at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 05-02 18:36:33.113: E/AndroidRuntime(902): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 05-02 18:36:33.113: E/AndroidRuntime(902): at java.net.InetAddress.getAllByName(InetAddress.java:220) 05-02 18:36:33.113: E/AndroidRuntime(902): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)

so anybody knows where im going wrong

This is the key error in your LogCat message --> android.os.NetworkOnMainThreadException. You should move your network access to a background thread. You can use either an IntentService, AsynchTask, Handler, etc. to accomplish this.

Check out http://developer.android.com/training/articles/perf-anr.html for more information about why it is important to do network ops on a separate thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM