3

I'm trying to get some information from this website:

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

The table structure is:

<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>

My code:

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());
    }

}
}

What I expect to see:

Take the Eastern Suburbs and Illawarra train (CityRail)                                 

Dep: 12:35pm ; Hurstville Station Platform 3

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

What I have tried:

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

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

Because the data actually in

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

So I guess I could not just use this(a space between dataTbl and widthcol2and3):

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

So I tried:

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

But every time I try to run the test app in the emulator, I got

The application has stopped unexpectedly. Please try again.

The log as shown below:

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

It's seem like the jsoup could not find the right class.

I think my selector-syntax is wrong, but I'v look at Use selector-syntax to find elements, I still can not solve this problem.

Please help me on this issue.

Yeasiz
  • 33
  • 4
  • 2
    *"It's seem like the jsoup could not find the right class."* No, the real problem here is that Android cannot find Jsoup. This means that you haven't properly put it in the runtime classpath. This problem is totally unrelated to Jsoup itself, it haven't had *any* chance to run (simply because Android couldn't find its class definition, exactly as the exception message is trying to tell you). – BalusC May 29 '12 at 06:06
  • ADT17? People usually link to an answer here on stackoverflow or to this [bug record](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 May 29 '12 at 06:54
  • Thanks for the tips BalusC, but actually I have put the jsoup-1.6.3.jar to my projectRoot/lib/ , and added to my build path, the jsoup-1.6.3.jar shows in my project Properties -> Java Build Path -> Libraries, here is the screenshot: http://real.gd/w8zy – Yeasiz May 29 '12 at 07:03
  • about the lilbrary thing, try renaming "lib" folder to "libs" (http://stackoverflow.com/questions/2247998/noclassdeffounderror-eclipse-and-android) I had the same problem once, this solution worked – Arie May 29 '12 at 07:24
  • Thank you, Arie. Changed the lib name to libs and solved the problem! – Yeasiz May 31 '12 at 02:17

2 Answers2

1

What exception you get, at what line?

For starters, you don't do this in catch():

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

It'll only execute if there's error during connecting, and if there is, then doc will always be null at this point.

Second, the code you provided throws connection timeout exception when I try it. Try this (works for me):

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();

ALso, you try to select elements by id: "div#boxbody", where "boxbody" is a part of class name. I opened the link you provided and there is more than one div element with class name containing the word "boxbody", and it's not the whole name of the class. I think the class name you are interested in will be "boxbody iewfix". Maybe it'll work, but I noticed that sometimes Jsoup reacts strangely to spaces (getElementsByClass("boxbody iewfix") doesn't work for me).

I don't like select, I usually make too many mistakes while using it, so instead I'd do:

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

Then

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

It will return:

"Mode Details Take the Eastern Suburbs and Illawarra train (CityRail) Dep: 5:05pm  Hurstville Station Platform 3 Arr: 5:31pm  Town Hall Station Platform 5, Sydney Map this trip Route Diagram Alternative Times"

Arie
  • 4,869
  • 2
  • 28
  • 49
0

That shall give you what you expect.

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");