2011-08-16 20 views
0

我正在嘗試使用AsyncTask,因爲活動強制關閉,因爲它需要在主線程上很長時間。如何使用AsyncTask進行Jsoup解析器?

以下是我希望在DoInBackGround使用..

} 
class fetcher extends AsyncTask<Void,Void, Void>{ 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     Document doc = null; 
     try { 
      doc = Jsoup.connect(url).get(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     TextView article = (TextView)findViewById(R.id.releaseInfo); 
     final TextView featuresText = (TextView)findViewById(R.id.features); 

     // Get the overview div 
     Element overview = doc.select("div#object-overview").last(); 
     Element featureList = doc.select("div.callout-box").last(); 

     Elements features = featureList.select("li"); 
     ListIterator<Element> featList = features.listIterator(); 
     while (featList.hasNext()) { 
      featuresText.setText("Features: " + featList.next().text() + "\n"); 

     } 


     // Get the paragraph element 
     Element paragraph = overview.select("p").last(); 
     System.out.println(paragraph.text()); 

     article.setText(paragraph.text()); 


     return null; 
    } 

} 

}

它總是強行關閉我不知道爲什麼?

編輯:這是調試錯誤的,我得到

08-16 18:52:59.547: WARN/System.err(27209): java.net.SocketTimeoutException 
08-16 18:52:59.547: WARN/System.err(27209):  at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:564) 
08-16 18:52:59.547: WARN/System.err(27209):  at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:61) 
08-16 18:52:59.547: WARN/System.err(27209):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279) 
08-16 18:52:59.547: WARN/System.err(27209):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readServerResponse(HttpURLConnectionImpl.java:1351) 
08-16 18:52:59.547: WARN/System.err(27209):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.sendRequest(HttpURLConnectionImpl.java:1339) 
08-16 18:52:59.547: WARN/System.err(27209):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1656) 
08-16 18:52:59.547: WARN/System.err(27209):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649) 
08-16 18:52:59.557: WARN/System.err(27209):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374) 

更多...

08-16 18:52:59.577: ERROR/AndroidRuntime(27209): FATAL EXCEPTION: AsyncTask #2 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209): java.lang.RuntimeException: An error occured while executing doInBackground() 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at java.lang.Thread.run(Thread.java:1096) 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209): Caused by: java.lang.NullPointerException 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at com.fttech.htmlParser.HtmlparserExampleActivity$getGames.doInBackground(HtmlparserExampleActivity.java:156) 
08-16 18:52:59.577: ERROR/AndroidRuntime(27209):  at com.fttech.htmlParser.HtmlparserExampleActivity$getGames.doInBackground(HtmlparserExampleActivity.java:1) 

的NullPointerException異常點,這種方法在doInBackground()

   Elements games = doc.select("tr> td.indexList1, tr > td.indexList2"); 

編輯:在android蜂窩上運行時出現此錯誤

08-16 19:04:01.600: ERROR/AndroidRuntime(7302): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
+0

logcat中的錯誤是什麼,請顯示錯誤。 –

+0

查看我的編輯 – yoshi24

回答

1

http://download.oracle.com/javase/1.5.0/docs/api/java/net/SocketTimeoutException.html

您連接已超時。這就是爲什麼NPE在您試圖從空對象中嘗試.select("something")時拋出的原因。 docDocument對象爲空,因爲沒有從url返回的數據。因此AsyncTask實例正在拋出RuntimeException

檢查您是否在代理後面。檢查您是否可以在瀏覽器中訪問該網址。檢查模擬器的DNS。在您的清單中檢查權限INTERNET。

Plus:更新UI必須在UI線程中完成。即你需要做的是,在onPostExecute()

Еdit:

class fetcher extends AsyncTask<Void,Void, Void>{ 
    //HERE DECLARE THE VARIABLES YOU USE FOR PARSING 
    private Element overview=null; 
    private Element featureList=null; 
    private Elements features=null; 
    private Element paragraph =null; 
    @Override 
    protected Void doInBackground(Void... arg0) { 
     Document doc = null; 
     try { 
      doc = Jsoup.connect(url).get(); 
     overview = doc.select("div#object-overview").last(); 
     featureList = doc.select("div.callout-box").last(); 

     features = featureList.select("li"); 
     paragraph = overview.select("p").last(); 
     System.out.println(paragraph.text()); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // Get the paragraph element 
     // article.setText(paragraph.text()); I comment this out because you cannot update ui from non-ui thread, since doInBackground is running on background thread. 


     return null; 
    } 
    @Override 
    protected Void onPostExecute(Void result) 
    { 

    TextView article = (TextView)findViewById(R.id.releaseInfo); 
     final TextView featuresText = (TextView)findViewById(R.id.features); 
     for(Element e: features) 
     { 
     //setText() 
     } 

} 

} 
+0

好吧,如果我在調試設備上有很好的服務,它就會運行!但一分鐘我沒有服務或服務極低,像沒有酒吧,它給出了時間錯誤 – yoshi24

+0

這可以解釋爲什麼文檔返回null – yoshi24

+0

在try catch語句中添加代碼。你可以嘗試循環一個while循環,如'Document doc = null; ''while(doc == null)''try' this catch' –

3

與您的代碼示例另一個問題:doInBackground()是在一個單獨的線程運行。您正試圖從後臺線程操縱兩個TextViews,這在Android中是不允許的。

Android SDK docs

此外,Andoid UI工具包不是線程安全的。因此,您必須不要從工作線程操縱您的用戶界面 - 您必須從UI線程對您的用戶界面進行所有 操作。因此,有 只是兩個規則Android的單線程模型:

不要阻塞UI線程

不要從UI線程外部訪問Android的UI工具包

你必須將呼叫轉移到TextView回到您的主線程。 AsyncTask爲您提供了兩種內置方式來執行此操作:onProgressUpdate()onPostExecute()。您放入這些方法中的代碼將在主UI線程上運行。

+0

正確,但這裏有另一回事。首先,由於doc空對象引發了NPE,因此AsyncTask引發了運行時異常 –