我尝试将 Async 与 JSoup 连接起来。我想从 URL 中获取标题,但捕获异常。但我不明白为什么
我的 LogCat 是:
12-14 23:54:19.612: I/Process(568): Sending signal. PID: 568 SIG: 9
12-14 23:54:55.141: D/gralloc_goldfish(632): Emulator without GPU emulation detected.
12-14 23:55:01.051: I/dalvikvm(632): Could not find method org.jsoup.Jsoup.connect, referenced from method com.example.myexample.MainActivity$MyTask.doInBackground
12-14 23:55:01.081: W/dalvikvm(632): VFY: unable to resolve static method 3465: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection;
12-14 23:55:01.081: D/dalvikvm(632): VFY: replacing opcode 0x71 at 0x000a
12-14 23:55:03.111: W/dalvikvm(632): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
12-14 23:55:03.181: E/AndroidRuntime(632): FATAL EXCEPTION: AsyncTask #1
12-14 23:55:03.181: E/AndroidRuntime(632): java.lang.RuntimeException: An error occured while executing doInBackground()
12-14 23:55:03.181: E/AndroidRuntime(632): at android.os.AsyncTask$3.done(AsyncTask.java:278)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-14 23:55:03.181: E/AndroidRuntime(632): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.lang.Thread.run(Thread.java:856)
12-14 23:55:03.181: E/AndroidRuntime(632): Caused by: java.lang.NoClassDefFoundError: org.jsoup.Jsoup
12-14 23:55:03.181: E/AndroidRuntime(632): at com.example.myexample.MainActivity$MyTask.doInBackground(MainActivity.java:50)
12-14 23:55:03.181: E/AndroidRuntime(632): at com.example.myexample.MainActivity$MyTask.doInBackground(MainActivity.java:1)
12-14 23:55:03.181: E/AndroidRuntime(632): at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-14 23:55:03.181: E/AndroidRuntime(632): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-14 23:55:03.181: E/AndroidRuntime(632): ... 5 more
据我所知,我对 section 有一个错误doInBackground
。当我将 params[0] 更改为 params 我也有错误
public class MainActivity extends Activity {
MyTask mt;
TextView tvInfo;
String URL="http://www.yandex.ru/";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvInfo = (TextView) findViewById(R.id.tvInfo);
}
public void onclick(View v) {
mt = new MyTask();
mt.execute(URL);
}
class MyTask extends AsyncTask<String, Void, String> {
Document doc;
String title=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
tvInfo.setText("Please wait");
}
@Override
protected String doInBackground(String... params) {
try {
TimeUnit.SECONDS.sleep(2);
doc = Jsoup.connect(params[0]).get();
String title = doc.title();
Log.d("AsyncTask doInBackground","URL: " + params[0]);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tvInfo.setText(title);
}
}
}