简体   繁体   中英

JSoup HTML parse in android

I am trying to build an app that is able to connect to a server and fill in credential in order to authenticate(like web e-mail access). After searching a bit I found that Jsoup is suitable for such tasks. I have searched topics on how to include the Jsoup into an android application and followed all the tips there:

  1. Download the Jsoup jar file
  2. Added the Jsoup jar file in the Project->Properties->Java Build paths->Libraries->Add external Jars
  3. Fixed the export of the jar

Here is the code:

package com.example.jsoup1;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
import org.jsoup.*;
import org.jsoup.nodes.Document;
public class MainActivity extends Activity {
Document doc;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {

        // need http protocol
    doc = Jsoup.connect("http://google.com").get();

         //get page title
        String title = doc.title();
        Toast.makeText(getBaseContext(),title,Toast.LENGTH_SHORT).show();


    } catch (IOException e) {
        e.printStackTrace();
    }   
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

The problem is that I get a runtime error after starting the application on the phone. It seems that dlavik cannot find some class used by the Jsoup I believe. The java.awt package as I lokked for it seems not to be part of the android sdk, but somehow it is used by the Jsoup. So what can I do to be able to run the application at least?

Here is the log:

09-17 10:53:17.134: V/ActivityThread(28297): Class path: /data/app/com.example.jsoup1-2.apk, JNI path: /data/data/com.example.jsoup1/lib
09-17 10:53:17.203: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/awt/Container;)
09-17 10:53:17.208: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/awt/Frame;)
09-17 10:53:17.209: E/dalvikvm(28297): Could not find class 'java.awt.Frame', referenced from method com.trend.iwss.jscan.runtime.Session.getContainingFrame
09-17 10:53:17.209: W/dalvikvm(28297): VFY: unable to resolve instanceof 690 (Ljava/awt/Frame;) in Lcom/trend/iwss/jscan/runtime/Session;
09-17 10:53:17.209: D/dalvikvm(28297): VFY: replacing opcode 0x20 at 0x0004
09-17 10:53:17.210: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/applet/Applet;)
09-17 10:53:17.211: E/dalvikvm(28297): Could not find class 'java.applet.Applet', referenced from method com.trend.iwss.jscan.runtime.Session.setAppletInstance
09-17 10:53:17.211: W/dalvikvm(28297): VFY: unable to resolve instanceof 681 (Ljava/applet/Applet;) in Lcom/trend/iwss/jscan/runtime/Session;
09-17 10:53:17.211: D/dalvikvm(28297): VFY: replacing opcode 0x20 at 0x0005
09-17 10:53:17.212: E/dalvikvm(28297): Could not find class 'java.applet.Applet', referenced from method com.trend.iwss.jscan.runtime.Session.setAppletInstance
09-17 10:53:17.212: W/dalvikvm(28297): VFY: unable to resolve check-cast 681 (Ljava/applet/Applet;) in Lcom/trend/iwss/jscan/runtime/Session;
09-17 10:53:17.212: D/dalvikvm(28297): VFY: replacing opcode 0x1f at 0x000d
09-17 10:53:17.213: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/awt/Window;)
09-17 10:53:17.214: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/awt/Dialog;)
09-17 10:53:17.214: E/dalvikvm(28297): Could not find class 'java.awt.Window', referenced from method com.trend.iwss.jscan.runtime.Session.closeAllActiveWindows
09-17 10:53:17.215: W/dalvikvm(28297): VFY: unable to resolve check-cast 697 (Ljava/awt/Window;) in Lcom/trend/iwss/jscan/runtime/Session;
09-17 10:53:17.215: D/dalvikvm(28297): VFY: replacing opcode 0x1f at 0x0012
09-17 10:53:17.215: E/dalvikvm(28297): Could not find class 'java.awt.Dialog', referenced from method com.trend.iwss.jscan.runtime.Session.closeAllOpenDialogs
09-17 10:53:17.216: W/dalvikvm(28297): VFY: unable to resolve check-cast 687 (Ljava/awt/Dialog;) in Lcom/trend/iwss/jscan/runtime/Session;
09-17 10:53:17.216: D/dalvikvm(28297): VFY: replacing opcode 0x1f at 0x0012
09-17 10:53:17.216: E/dalvikvm(28297): Could not find class 'java.applet.Applet', referenced from method com.trend.iwss.jscan.runtime.Session.getActiveFrame
09-17 10:53:17.217: W/dalvikvm(28297): VFY: unable to resolve check-cast 681 (Ljava/applet/Applet;) in Lcom/trend/iwss/jscan/runtime/Session;
09-17 10:53:17.217: D/dalvikvm(28297): VFY: replacing opcode 0x1f at 0x0011
09-17 10:53:17.217: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/awt/Frame;)
09-17 10:53:17.218: I/dalvikvm(28297): Could not find method java.awt.Component.isDisplayable, referenced from method com.trend.iwss.jscan.runtime.Session.getActiveFrame
09-17 10:53:17.218: W/dalvikvm(28297): VFY: unable to resolve virtual method 5008: Ljava/awt/Component;.isDisplayable ()Z
09-17 10:53:17.219: D/dalvikvm(28297): VFY: replacing opcode 0x6e at 0x005a
09-17 10:53:17.220: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/applet/Applet;)
09-17 10:53:17.220: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/awt/Window;)
09-17 10:53:17.221: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/awt/Dialog;)
09-17 10:53:17.221: W/dalvikvm(28297): VFY: unable to find class referenced in signature (Ljava/applet/Applet;)
09-17 10:53:17.224: E/dalvikvm(28297): Could not find class 'java.applet.Applet', referenced from method com.trend.iwss.jscan.runtime.PolicyRuntime.report
09-17 10:53:17.225: W/dalvikvm(28297): VFY: unable to resolve check-cast 681 (Ljava/applet/Applet;) in Lcom/trend/iwss/jscan/runtime/PolicyRuntime;
09-17 10:53:17.225: D/dalvikvm(28297): VFY: replacing opcode 0x1f at 0x0033
09-17 10:53:17.226: W/dalvikvm(28297): Unable to resolve superclass of Lcom/trend/iwss/jscan/runtime/BaseDialog; (687)
09-17 10:53:17.227: W/dalvikvm(28297): Link of class 'Lcom/trend/iwss/jscan/runtime/BaseDialog;' failed
09-17 10:53:17.227: W/dalvikvm(28297): Unable to resolve superclass of Lcom/trend/iwss/jscan/runtime/AllowDialog; (642)
09-17 10:53:17.227: W/dalvikvm(28297): Link of class 'Lcom/trend/iwss/jscan/runtime/AllowDialog;' failed
09-17 10:53:17.227: I/dalvikvm(28297): Could not find method com.trend.iwss.jscan.runtime.AllowDialog.make, referenced from method com.trend.iwss.jscan.runtime.PolicyRuntime.showAllowDialog
09-17 10:53:17.228: W/dalvikvm(28297): VFY: unable to resolve static method 4622: Lcom/trend/iwss/jscan/runtime/AllowDialog;.make (Ljava/lang/String;Lcom/trend/iwss/jscan/runtime/Session;I)Lcom/trend/iwss/jscan/runtime/AllowDialog;
09-17 10:53:17.228: D/dalvikvm(28297): VFY: replacing opcode 0x71 at 0x0014
09-17 10:53:17.229: W/dalvikvm(28297): Unable to resolve superclass of Lcom/trend/iwss/jscan/runtime/BaseDialog; (687)
09-17 10:53:17.229: W/dalvikvm(28297): Link of class 'Lcom/trend/iwss/jscan/runtime/BaseDialog;' failed
09-17 10:53:17.229: W/dalvikvm(28297): Unable to resolve superclass of Lcom/trend/iwss/jscan/runtime/OkDialog; (642)
09-17 10:53:17.230: W/dalvikvm(28297): Link of class 'Lcom/trend/iwss/jscan/runtime/OkDialog;' failed
09-17 10:53:17.230: I/dalvikvm(28297): Could not find method com.trend.iwss.jscan.runtime.OkDialog.make, referenced from method com.trend.iwss.jscan.runtime.PolicyRuntime.showOKDialog
09-17 10:53:17.230: W/dalvikvm(28297): VFY: unable to resolve static method 4818: Lcom/trend/iwss/jscan/runtime/OkDialog;.make (Ljava/lang/String;Lcom/trend/iwss/jscan/runtime/Session;Ljava/lang/String;Z)Lcom/trend/iwss/jscan/runtime/OkDialog;
09-17 10:53:17.230: D/dalvikvm(28297): VFY: replacing opcode 0x71 at 0x0016
09-17 10:53:17.232: E/dalvikvm(28297): Could not find class 'java.applet.Applet', referenced from method com.trend.iwss.jscan.runtime.PolicyRuntime.stopApplets
09-17 10:53:17.232: W/dalvikvm(28297): VFY: unable to resolve check-cast 681 (Ljava/applet/Applet;) in Lcom/trend/iwss/jscan/runtime/PolicyRuntime;
09-17 10:53:17.232: D/dalvikvm(28297): VFY: replacing opcode 0x1f at 0x0018
09-17 10:53:17.235: W/dalvikvm(28297): Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lorg/jsoup/Connection$Method;
09-17 10:53:17.235: D/AndroidRuntime(28297): Shutting down VM
09-17 10:53:17.236: W/dalvikvm(28297): threadid=1: thread exiting with uncaught exception (group=0x4101c8a8)
09-17 10:53:17.238: E/AndroidRuntime(28297): FATAL EXCEPTION: main
09-17 10:53:17.238: E/AndroidRuntime(28297): java.lang.ExceptionInInitializerError
09-17 10:53:17.238: E/AndroidRuntime(28297):    at org.jsoup.helper.HttpConnection$Request.<init>(HttpConnection.java:313)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at org.jsoup.helper.HttpConnection$Request.<init>(HttpConnection.java:299)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at org.jsoup.helper.HttpConnection.<init>(HttpConnection.java:41)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:26)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at org.jsoup.Jsoup.connect(Jsoup.java:73)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at com.example.jsoup1.MainActivity.onCreate(MainActivity.java:23)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at android.app.Activity.performCreate(Activity.java:5247)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2225)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at android.os.Looper.loop(Looper.java:153)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at android.app.ActivityThread.main(ActivityThread.java:5071)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at java.lang.reflect.Method.invokeNative(Native Method)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at java.lang.reflect.Method.invoke(Method.java:511)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at dalvik.system.NativeStart.main(Native Method)
09-17 10:53:17.238: E/AndroidRuntime(28297): Caused by: java.lang.NoClassDefFoundError: java.applet.Applet
09-17 10:53:17.238: E/AndroidRuntime(28297):    at com.trend.iwss.jscan.runtime.Session.setAppletInstance(Session.java:88)
09-17 10:53:17.238: E/AndroidRuntime(28297):    at org.jsoup.Connection$Method.<clinit>(Connection.java)
09-17 10:53:17.238: E/AndroidRuntime(28297):    ... 20 more

Please consider the below code which is working good for me

class BackGroundTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... params) {

            try {
                URL url= new URL("http://www.google.com");
                Document doc = Jsoup.connect(url.toString()).get();
                String paragaph=doc.title();

                return Paragraph;
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            System.out.println(result);
            super.onPostExecute(result);
        }

    }

in your onCreate()

new BackGroundTask().execute();

enable Internet permission

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM