0

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 i.e 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

x-code
  • 608
  • 1
  • 10
  • 30
  • Are you doing IO stuff on the UI thread? Your code is not complete. That is the first guess. – David Olsson May 02 '13 at 13:19
  • Duplicate: http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – laalto May 02 '13 at 13:26
  • @David im just displaying it in 3 textviews, which are embedded in one single activity, im a brand new to android, mate UI thread? is it from mars :( – x-code May 02 '13 at 13:27
  • @user1944477 You are most likely doing network operations on the UI (user interface) thread, which isn't allowed. Please check the other links, there are loads of examples and errors like this. – David Olsson May 02 '13 at 13:45
  • SOLVED IT!!! mainly bcoz of asynctask and this thread http://stackoverflow.com/questions/16120234/how-to-use-inputstream-as-params-in-android-asynctask and guys THANK U ALL – x-code May 02 '13 at 14:51

1 Answers1

1

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.

Erik
  • 103
  • 7