java - 使用雅虎财经检索股票报价

标签 java android yahoo-finance

我在提交时有一个带有按钮的表单,我从雅虎财经 API 检索股票信息,并使用它显示到三个 TextView

我使用的网址 http://download.finance.yahoo.com/d/quotes.csv?s=goog&f=sl1p2

我的按钮事件处理程序是

          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();
            }

所以我注意到直到第一行似乎没有问题,即 url = new URL...,(即使是通过浏览器完成的裸http请求,也确实返回了我需要的信息)

我的 list 条目看起来像

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

下面是 logcat 输出

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)

所以任何人都知道我哪里出了问题

最佳答案

这是 LogCat 消息中的关键错误 --> android.os.NetworkOnMainThreadException。您应该将网络访问移至后台线程。您可以使用 IntentService、AsynchTask、Handler 等来完成此操作。

查看http://developer.android.com/training/articles/perf-anr.html了解有关为什么在单独的线程上执行网络操作很重要的更多信息。

关于java - 使用雅虎财经检索股票报价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16339045/

相关文章:

java - 如何解决java中的 Exception in thread "TimerQueue"Exception in thread "AWT-EventQueue-0"java.lang.IllegalMonitorStateException 之类的错误?

java - 如何将事件从 MainActivity onCreate 发送到 React Native?

java - Spring TransactionalTemplate 不会在异常时回滚

android - 错误: Unknown Issue with Google Play Services

php - 如何使用 PHP 将 CSV 文件读入 MySQL

swift - 我正在努力寻找一种方法将 IPO 股票从雅虎财经 API 中撤出

java - 修剪字符串中空格的最佳方法

android - 如何制作具有多种语言的自定义键盘..?

android - Hello World App 无法在三星手机上运行

python - 从雅虎财经获取数据到 sqlite