[英]JSoup HTML parse in android
我正在尝试构建一个能够连接到服务器并填写凭据以进行身份验证的应用程序(例如Web电子邮件访问)。 经过一番搜索,我发现Jsoup适用于此类任务。 我搜索了有关如何将Jsoup包含到android应用程序中的主题,并按照那里的所有提示进行操作:
这是代码:
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;
}
}
问题是在手机上启动应用程序后出现运行时错误。 看来dlavik找不到我相信的Jsoup使用的某些类。 我所喜欢的java.awt软件包似乎不是android sdk的一部分,但是Jsoup使用了它。 那么,至少可以运行该应用程序怎么办?
这是日志:
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
请考虑以下对我有用的代码
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);
}
}
在你的onCreate()中
新的BackGroundTask()。execute();
启用互联网权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.