错误异步任务doInBackground()& JSoup

时间:2012-12-14 15:55:27

标签: android android-asynctask jsoup

我尝试将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

据我所知,我对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);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

将params用作数组,params[0] params[1] ...是正确的。

您是否可以添加常规异常catch以查看有关JSoup连接错误的更多细节?

另外,记录params [0]的值以确保它是有效的URL:

Log.d("AsyncTask doInBackground","URL: " + params[0]);

虽然没有它们就无法编译项目,但请确保导入正确的Java JSoup库。您的错误说它无法找到静态方法connection

import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;