Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Jsoup select() error

Tags:

android

jsoup

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.

like image 827
Yeasiz Avatar asked Dec 03 '25 16:12

Yeasiz


1 Answers

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"

like image 93
Arie Avatar answered Dec 06 '25 06:12

Arie