0

我的股票报价申请出现错误。我有一个应用程序,您可以在其中输入您的股票(如股票市场)代码,然后用两个按钮列出它。一个按钮显示报价,另一个按钮查看来自网络的更多信息。网络功能很好,但是当我点击报价按钮时应用程序崩溃了。

我认为这是有问题的方法。

@Override
protected String doInBackground(String... args) {

        try {

            URL url = new URL(args[0]);

            URLConnection connection;
            connection = url.openConnection();

            HttpURLConnection httpConnection = (HttpURLConnection) connection;

            int responseCode = httpConnection.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK) {

                InputStream in = httpConnection.getInputStream();

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                DocumentBuilder db = dbf.newDocumentBuilder();

                Document dom = db.parse(in);

                Element docEle = dom.getDocumentElement();

                NodeList nl = docEle.getElementsByTagName("quote");

                if(nl != null &&  nl.getLength() > 0) {

                    for(int i=0; i < nl.getLength(); i++){

                        StockInfo theStock = getStockInformation(docEle);

                        name = theStock.getName();
                        yearLow = theStock.getYearLow();
                        yearHigh = theStock.getYearHigh();
                        daysLow = theStock.getDaysLow();
                        daysHigh = theStock.getDaysHigh();
                        lastTradePriceOnly = theStock.getLastTradePriceonly();
                        change = theStock.getChange();
                        daysRange = theStock.getDaysRange();

                    }

                }

            }

        }

        catch (MalformedURLException e) {
                Log.d(TAG, "MalformedURLException", e);
            } catch (IOException e) {
                Log.d(TAG, "IOException", e);
            } catch (ParserConfigurationException e) {
                Log.d(TAG, "Parser Configuration Exception", e);
            } catch (SAXException e) {
                Log.d(TAG, "SAX Exception", e);
            }

        finally { }

        return null;
    }

LogCat 显示我的 StockInfoActivity 类的 doInBackground() 中有一个 OutOfBoundsException,但我找不到它。将不胜感激找到问题的任何帮助。

10-02 02:59:52.738: E/AndroidRuntime(26553): FATAL EXCEPTION: AsyncTask #4
10-02 02:59:52.738: E/AndroidRuntime(26553): java.lang.RuntimeException: An error occured while executing doInBackground()
10-02 02:59:52.738: E/AndroidRuntime(26553):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at java.lang.Thread.run(Thread.java:841)
10-02 02:59:52.738: E/AndroidRuntime(26553): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
10-02 02:59:52.738: E/AndroidRuntime(26553):    at com.example.stockquote.StockInfoActivity$MyAsyncTask.doInBackground(StockInfoActivity.java:93)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at com.example.stockquote.StockInfoActivity$MyAsyncTask.doInBackground(StockInfoActivity.java:1)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-02 02:59:52.738: E/AndroidRuntime(26553):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-02 02:59:52.738: E/AndroidRuntime(26553):    ... 4 more

抱歉,我很难隔离问题。

4

0 回答 0