サーバーに接続し、認証のために資格情報を入力できるアプリを構築しようとしています (Web 電子メール アクセスなど)。少し調べてみると、Jsoup がそのようなタスクに適していることがわかりました。Jsoup を Android アプリケーションに組み込む方法に関するトピックを検索し、そこにあるすべてのヒントに従いました。
- Jsoup jar ファイルをダウンロードします。
- Project->Properties->Java Build paths->Libraries->Add external Jars に Jsoup jar ファイルを追加しました。
- jar のエクスポートを修正しました
コードは次のとおりです。
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