2012-05-29 84 views
3

我想從這個網站上的一些信息:的Android Jsoup選擇()錯誤

http://www.131500.com.au/plan-your-trip/trip-planner?itd_name_origin=hurstville&itd_name_destination=town+hall

表結構是:

<td headers="header2"> 
    Take the Eastern Suburbs and Illawarra train (CityRail)         
    <br /> 
    <b>Dep: 12:35pm&nbsp; Hurstville Station Platform 3</b> 
    <br /> 
    <b>Arr: 1:06pm&nbsp; Town Hall Station Platform 5, Sydney</b> 
    <br /> 
</td> 

我的代碼:

import java.io.IOException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.select.Elements; 

import android.app.Activity; 
import android.os.Bundle; 

public class JsouptestActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    jsouptest(); 
} 

public void jsouptest() { 
    Document doc = null; 
    try { 
     doc = Jsoup 
       .connect(
         "http://www.131500.com.au/plan-your-trip/trip-planner?itd_name_origin=hurstville&itd_name_destination=town+hall") 
       .get(); 
    } catch (IOException e) { 
     Elements tables = doc.select("div#boxbody"); 
     System.out.println(tables.get(0).text().toString()); 
    } 

} 
} 
我想要看到的是:
Take the Eastern Suburbs and Illawarra train (CityRail)         

Dep: 12:35pm ; Hurstville Station Platform 3 

Arr: 1:06pm ; Town Hall Station Platform 5, Sydney 

我曾嘗試:

Elements tables = doc.select("div#boxbody table#dataTbl"); 

Elements tables = doc.select("div#boxbody table#dataTbl+widthcol2and3"); 

由於數據實際上

<table class="dataTbl widthcol2and3" cellspacing="0" style="margin:0px ! important;border-right:0px none;" summary="Search Results Details"> 

所以我想我不能只是用這個(dataTbl之間的空間和widthcol2and3):

Elements tables = doc.select("div#boxbody table#dataTbl widthcol2and3"); 

所以,我想:

Elements tables = doc.select("div#boxbody iewfix"); // and div#boxbody+iewfix 

但每次我試圖在模擬器運行測試程序的時候,我得到了

The application has stopped unexpectedly. Please try again. 

日誌如下圖所示:

05-29 15:58:42.575: W/dalvikvm(755): threadid=3: thread exiting with uncaught exception  (group=0x4001b188) 
05-29 15:58:42.575: E/AndroidRuntime(755): Uncaught handler: thread main exiting due to uncaught exception 
05-29 15:58:42.585: E/AndroidRuntime(755): java.lang.NoClassDefFoundError: org.jsoup.Jsoup 
05-29 15:58:42.585: E/AndroidRuntime(755): at com.yeasiz.jsouptest.JsouptestActivity.jsouptest(JsouptestActivity.java:25) 
05-29 15:58:42.585: E/AndroidRuntime(755): at com.yeasiz.jsouptest.JsouptestActivity.onCreate(JsouptestActivity.java:18) 
05-29 15:58:42.585: E/AndroidRuntime(755): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
05-29 15:58:42.585: E/AndroidRuntime(755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
05-29 15:58:42.585: E/AndroidRuntime(755): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
05-29 15:58:42.585: E/AndroidRuntime(755): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
05-29 15:58:42.585: E/AndroidRuntime(755): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
05-29 15:58:42.585: E/AndroidRuntime(755): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-29 15:58:42.585: E/AndroidRuntime(755): at android.os.Looper.loop(Looper.java:123) 
05-29 15:58:42.585: E/AndroidRuntime(755): at  android.app.ActivityThread.main(ActivityThread.java:4363) 
05-29 15:58:42.585: E/AndroidRuntime(755): at java.lang.reflect.Method.invokeNative(Native Method) 
05-29 15:58:42.585: E/AndroidRuntime(755): at java.lang.reflect.Method.invoke(Method.java:521) 
05-29 15:58:42.585: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
05-29 15:58:42.585: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
05-29 15:58:42.585: E/AndroidRuntime(755): at dalvik.system.NativeStart.main(Native Method) 
05-29 15:58:42.595: I/dalvikvm(755): threadid=7: reacting to signal 3 
05-29 15:58:42.595: E/dalvikvm(755): Unable to open stack trace file '/data/anr/traces.txt':  Permission denied 

它看起來像jsoup找不到正確的課程。

我覺得我的選擇器語法是錯誤的,但我看看Use selector-syntax to find elements,我仍然無法解決這個問題。

請幫我解決這個問題。

+2

*「好像jsoup找不到合適的類。」*不,這裏真正的問題是Android找不到Jsoup。這意味着你沒有把它放在運行時類路徑中。這個問題與Jsoup本身完全無關,它沒有任何運行機會(只是因爲Android無法找到它的類定義,正如異常消息試圖告訴你的那樣)。 – BalusC

+0

ADT17?人們通常在這裏鏈接到一個答案在stackoverflow或這[錯誤記錄](http://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status %20Owner%20Summary%20Stars&groupby =&sort =&id = 27490) – Jens

+0

感謝BalusC的提示,但實際上我已經將jsoup-1.6.3.jar放到了我的projectRoot/lib /中,並添加到了我的構建路徑中,jsoup- 1.6.3.jar顯示在我的項目Properties - > Java Build Path - > Libraries中,這裏是截圖:http://real.gd/w8zy – Yeasiz

回答

1

你得到什麼異常,在哪一行?

對於初學者來說,你不抓做到這一點():

org.jsoup.select.Elements tables = doc.select("div#boxbody"); 
System.out.println(tables.get(0).text().toString()); 

如果連接過程中有錯誤,如果有,則文檔將永遠在這一點零它只會執行。

其次,您提供的代碼在嘗試時會引發連接超時異常。 試試這個(作品對我來說):

Document doc = null; 
InputStream is = null; 
String url = "http://www.131500.com.au/plan-your-trip/trip-planner?itd_name_origin=hurstville&itd_name_destination=town+hall"; 
is =new URL(url).openStream(); 
doc = org.jsoup.Jsoup.parse(is , "utf-8", url); 
is.close(); 

此外,您儘量選擇由ID元素: 「#DIV boxbody」,其中 「boxbody」 是類名稱的一部分。我打開了您提供的鏈接,並且包含單詞「boxbody」的類名稱不止一個div元素,而不是該類的全名。我認爲你感興趣的類名將是「boxbody iewfix」。也許它會工作,但我注意到,有時Jsoup奇怪地反應空間(getElementsByClass(「boxbody iewfix」)不適用於我)。

我不喜歡的選擇,而使用它,我通常做了太多的錯誤,所以不是我會做:

Elements tables = doc.getElementsByAttributeValueStarting("class", "boxbody"); //I checked, it works 

然後

tables.get(2).text(); // because the you're interested in third element which class name starts with "boxbody" 

它將返回:

「模式詳情乘東部郊區和伊拉瓦拉火車(城市鐵路)部長:下午5:05赫斯特維爾站3號站臺:下午5點31分市政廳站臺5號平臺,悉尼地圖此行路線Rout e圖替代時間「

0

這應該給你你所期望的。

Elements lines = doc.select("td[headers=header2]"); 
String linesToStr = lines.text(); 
String[] linestoStrArray = linesToStr.split("\n"); 

for (String line : linesToStrArray) 
    System.out.println(line + "\n");