2016-03-07 129 views
2

我正在使用下面的代碼。它在Android Studio中發生錯誤。你能幫忙嗎?根據我的理解,args [0]沒有得到一些值。這是爲什麼?如果args [0]沒有爲此選擇任何其他實現的值。java.lang.RuntimeException:執行doInBackground()時發生錯誤

import android.app.Activity; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.provider.DocumentsContract; 
import android.renderscript.Element; 
import android.util.Log; 
import android.widget.TextView; 

import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

import java.io.IOException; 
import java.io.InputStream; 
import java.lang.annotation.ElementType; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

public class StockInfoActivity extends Activity{ 
    private static final String TAG="StockQoute"; 

    static final String KEY_ITEM="quote"; 
    static final String KEY_YEAR_LOW="YearLow"; 
    static final String KEY_YEAR_HIGH="YearHigh"; 
    static final String KEY_DAYS_LOW="DaysLow"; 
    static final String KEY_DAYS_HIGH="DaysHigh"; 
    static final String KEY_CHANGE="Change"; 
    static final String KEY_LAST_TRADE="LastTradePriceOnly"; 
    static final String KEY_DAYS_CHANGE="DaysRange"; 

    TextView companyNameView; 
    TextView yearLowView ; 
    TextView yearHighView ; 
    TextView daysLowView ; 
    TextView daysHighView ; 
    TextView LastPriceView ; 
    TextView changeView  ; 
    TextView dailyPriceRangeView; 

    String name=""; 
    String yearLow=""; 
    String yearHigh=""; 
    String daysLow=""; 
    String daysHigh=""; 
    String lastTradePrice=""; 
    String change=""; 
    String daysRange=""; 

    String yahooURLFirst="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22"; 
    String yahooURLSecond="%22)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; 

    protected void onCreate (Bundle savedInstanceState){ 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.get_stock_qoute); 
     Intent intent=getIntent(); 
     String stockSymbol=intent.getStringExtra(MainActivity.STOCK_SYMBOL); 

     companyNameView =(TextView)findViewById(R.id.companyNameView); 
     yearLowView =(TextView)findViewById(R.id.yearLow); 
     yearHighView =(TextView)findViewById(R.id.yearHigh); 
     daysLowView =(TextView)findViewById(R.id.daysLow); 
     daysHighView =(TextView)findViewById(R.id.daysHigh); 
     LastPriceView =(TextView)findViewById(R.id.lastPrice); 
     changeView  =(TextView)findViewById(R.id.change); 
     dailyPriceRangeView =(TextView)findViewById(R.id.dailyPriceRange); 
     //Log.d(TAG, "Before URL creation" + stockSymbol); 
     final String yqlURL=yahooURLFirst+stockSymbol+yahooURLSecond; 
     //Log.d(TAG, "Before URL creation" + yqlURL); 
     new MyAsyncTask().execute(); 
    } 

    private class MyAsyncTask extends AsyncTask<String,String,String>{ 

     protected String doInBackground(String... args) { 
       try{ 
        String newstr=args[0]; 
        Log.d("Sync",newstr); 
        URL url= new URL(args[0]); 

        //Log.d("Sync1","We are here"); 
        URLConnection conn=url.openConnection(); 

        HttpURLConnection httpconn=(HttpURLConnection)conn; 

        int responseCode=httpconn.getResponseCode(); 

        if(responseCode==HttpURLConnection.HTTP_OK){ 
         InputStream in=httpconn.getInputStream(); 
         DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance(); 
         DocumentBuilder db=dbf.newDocumentBuilder(); 
         Document dom=db.parse(in); 
         org.w3c.dom.Element docEl= (org.w3c.dom.Element) dom.getDocumentElement(); 
         NodeList nl = docEl.getElementsByTagName("qoute"); 
         Log.d("Sync3","We are here"); 
         if(nl!=null && nl.getLength() > 0){ 
          for(int i=0;i<=nl.getLength();i++){ 
           StockInfo theStock=getStockInfo(docEl); 
           name=theStock.getName(); 
           yearLow=theStock.getYearLow(); 
           yearHigh=theStock.getYearHigh(); 
           daysLow=theStock.getDaysLow(); 
           daysHigh=theStock.getDaysHigh(); 
           lastTradePrice=theStock.getLastTradePriceonly(); 
           change=theStock.getChange(); 
           daysRange=theStock.getDaysRange(); 
          } 
         } 
        } 

      } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (ParserConfigurationException e) { 
        e.printStackTrace(); 
       } catch (SAXException e) { 
        e.printStackTrace(); 
       } 
finally {  } 
      return null; 
     } 

     protected void onPostExecute(String result){ 
      companyNameView.setText(name); 
      yearHighView.setText("Year High: " +yearHigh); 
      yearLowView.setText("Year Low: "+yearLow); 
      daysHighView.setText("Days High: "+daysHigh); 
      daysLowView.setText("Days Low:"+ daysLow); 
      LastPriceView.setText("Last Price"+ lastTradePrice); 
      changeView.setText("Change: "+change); 
      dailyPriceRangeView.setText("Daily Price Range: "+daysRange) ; 
     } 

private StockInfo getStockInfo(org.w3c.dom.Element entry){ 
    String stockName=getTextValue(entry,"Name"); 
    String stockYearLow=getTextValue(entry,"YearLow"); 
    String stockYearHigh=getTextValue(entry,"YearHigh"); 
    String stockDaysLow=getTextValue(entry,"DaysLow"); 
    String stockDaysHigh=getTextValue(entry,"DaysHigh"); 
    String stockLastTradePrice=getTextValue(entry,"LastTradePriceOnly"); 
    String stockChange=getTextValue(entry,"Change"); 
    String stockDaysRange=getTextValue(entry,"DaysRange"); 

    StockInfo theStock=new StockInfo(stockDaysLow 
      ,stockDaysHigh ,stockYearLow ,stockYearHigh ,stockName,stockLastTradePrice ,stockChange ,stockDaysRange); 
return theStock; 
} 
private String getTextValue(org.w3c.dom.Element Entry, String tagName) 
{ 
    String tagValueToReturn=null; 
    NodeList nl=Entry.getElementsByTagName(tagName); 
    if(nl!=null && nl.getLength() > 0){ 
     org.w3c.dom.Element ele=(org.w3c.dom.Element)nl.item(0); 
     tagValueToReturn=ele.getFirstChild().getNodeValue(); 
     } 
    return tagValueToReturn; 
} 
} 
} 

得到以下錯誤

FATAL EXCEPTION: AsyncTask #1 
Process: com.shravan.stockinfo, PID: 2188 
java.lang.RuntimeException: An error occured while executing doInBackground() 
    at android.os.AsyncTask$3.done(AsyncTask.java:304) 
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
    at java.lang.Thread.run(Thread.java:818) 
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=1 
    at com.shravan.stockinfo.StockInfoActivity$MyAsyncTask.doInBackground(StockInfoActivity.java:89) 
    at com.shravan.stockinfo.StockInfoActivity$MyAsyncTask.doInBackground(StockInfoActivity.java:85) 
    at android.os.AsyncTask$2.call(AsyncTask.java:292) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  
    at java.lang.Thread.run(Thread.java:818)  

回答

2

你是運行AsyncTask就像new MyAsyncTask().execute();這意味着你沒有傳遞任何參數。

doInBackground方法你想在String newstr=args[0];args陣列得到的值具有長度爲0

您需要啓動AsycTask像這樣

new MyAsyncTask().execute(yqlURL); 
+0

完成工作正常。但現在我沒有得到我的變量的任何價值。他們顯示空白。 –

+0

@ShravanYadav:你能證明你是如何傳遞它的嗎? – Rohit5k2

+0

上述代碼具有從xml中提取數據的所有功能。還有另一個java文件StockInfo只有變量和getter和setter。 –

1

您還沒有發送參數到你的異步任務,所以當你args[0]它覺得沒有什麼,記得要送參數這樣mytask.execute(params);

+0

現在工作正常。但我沒有得到我的變數的任何價值。他們顯示空白。 –

0
new MyAsyncTask().execute(); 

你逝去這裏沒有什麼,這裏沒有任何爭論

protected String doInBackground(String... args) { 
       try{ 
        String newstr=args[0]; 
        Log.d("Sync",newstr); 
        URL url= new URL(args[0]); 
.... 
} 

還是想在這裏讀String newstr=args[0];,只是通過 new MyAsyncTask(yourString).execute();

0

你需要一個像

開始的AsyncTask時發送參數

新MyAsyncTask()執行(yqlURL)。

否則它會拋出ArrayIndexOutOfBoundsException,因爲args大小爲零。

+0

爲什麼我被拒絕投票? – nitinkumarp

相關問題