0

我试图解析一个 div 或一个表(对我来说几乎相同,因为表在 div 内)。

所以我检查了这个问题:Load a div into a webview on Android

但问题是我使用的 div 没有 ID。但它有一个类。

所以它就像:<div class='CSSTableGenerator' style='width:820px'>

我该怎么做才能在 webview 上单独显示 div?

代码:

Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Elements ele = doc.getElementsByClass("CSSTableGenerator");

        String html = ele.toString();
        String mime = "text/html";
        String encoding = "utf-8";
        webView.loadData(html, mime, encoding);

迷恋的Logcat:

08-22 13:22:24.304: D/libEGL(24034): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-22 13:22:24.722: D/libEGL(24034): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-22 13:22:24.777: D/libEGL(24034): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-22 13:22:25.230: D/OpenGLRenderer(24034): Enabling debug mode 0
08-22 13:22:35.543: I/dalvikvm(24034): Could not find method org.jsoup.Jsoup.connect, referenced from method com.example.owl.Results.onCreate
08-22 13:22:35.543: W/dalvikvm(24034): VFY: unable to resolve static method 4835: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection;
08-22 13:22:35.543: D/dalvikvm(24034): VFY: replacing opcode 0x71 at 0x0054
08-22 13:22:35.543: I/dalvikvm(24034): Could not find method org.jsoup.nodes.Document.getElementsByClass, referenced from method com.example.owl.Results.onCreate
08-22 13:22:35.543: W/dalvikvm(24034): VFY: unable to resolve virtual method 4836: Lorg/jsoup/nodes/Document;.getElementsByClass (Ljava/lang/String;)Lorg/jsoup/select/Elements;
08-22 13:22:35.543: D/dalvikvm(24034): VFY: replacing opcode 0x6e at 0x005e
08-22 13:22:36.175: W/Bundle(24034): Key name expected String but value was a android.text.SpannableString.  The default value <null> was returned.
08-22 13:22:36.414: W/Bundle(24034): Attempt to cast generated internal exception:
08-22 13:22:36.414: W/Bundle(24034): java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String
08-22 13:22:36.414: W/Bundle(24034):    at android.os.Bundle.getString(Bundle.java:1069)
08-22 13:22:36.414: W/Bundle(24034):    at android.content.Intent.getStringExtra(Intent.java:4350)
08-22 13:22:36.414: W/Bundle(24034):    at com.example.owl.Results.onCreate(Results.java:25)
08-22 13:22:36.414: W/Bundle(24034):    at android.app.Activity.performCreate(Activity.java:5104)
08-22 13:22:36.414: W/Bundle(24034):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-22 13:22:36.414: W/Bundle(24034):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
08-22 13:22:36.414: W/Bundle(24034):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
08-22 13:22:36.414: W/Bundle(24034):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
08-22 13:22:36.414: W/Bundle(24034):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-22 13:22:36.414: W/Bundle(24034):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 13:22:36.414: W/Bundle(24034):    at android.os.Looper.loop(Looper.java:137)
08-22 13:22:36.414: W/Bundle(24034):    at android.app.ActivityThread.main(ActivityThread.java:5227)
08-22 13:22:36.414: W/Bundle(24034):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:22:36.414: W/Bundle(24034):    at java.lang.reflect.Method.invoke(Method.java:511)
08-22 13:22:36.414: W/Bundle(24034):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-22 13:22:36.414: W/Bundle(24034):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-22 13:22:36.414: W/Bundle(24034):    at dalvik.system.NativeStart.main(Native Method)
08-22 13:22:36.433: D/AndroidRuntime(24034): Shutting down VM
08-22 13:22:36.433: W/dalvikvm(24034): threadid=1: thread exiting with uncaught exception (group=0x413e3930)
08-22 13:22:36.550: E/AndroidRuntime(24034): FATAL EXCEPTION: main
08-22 13:22:36.550: E/AndroidRuntime(24034): java.lang.NoClassDefFoundError: org.jsoup.Jsoup
08-22 13:22:36.550: E/AndroidRuntime(24034):    at com.example.owl.Results.onCreate(Results.java:29)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at android.app.Activity.performCreate(Activity.java:5104)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at android.os.Looper.loop(Looper.java:137)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at android.app.ActivityThread.main(ActivityThread.java:5227)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at java.lang.reflect.Method.invoke(Method.java:511)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-22 13:22:36.550: E/AndroidRuntime(24034):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

2

使用getElementsByClass()

元素可以有多个类(例如<div class="header round first">。这个方法检查每个类,所以你可以找到上面的el.getElementsByClass("header");.

在你的情况下

Elements ele = doc.getElementsByClass("CSSTableGenerator");
于 2013-08-22T09:50:11.480 回答