我的股票报价申请表上有错误。我有一个应用程序,您输入您的股票(如股票市场)代码,并将列出两个按钮。一个按钮显示报价,另一个按钮用于查看网络上的更多信息。网页功能很好,但是当我点击引号按钮时应用程序崩溃了。
我认为这是有问题的方法。
@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
抱歉,我很难解决这个问题。