JSoup不能与AsyncTask一起工作[英] JSoup doesn't work with AsyncTask

本文是小编为大家收集整理的关于JSoup不能与AsyncTask一起工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我注意到我的应用程序有一个有趣的东西(我在Asynctask中使用的jsoup)

之后我的应用程序崩溃了. logcat是:

 12-15 11:16:43.023: I/dalvikvm(371): Could not find method org.jsoup.Jsoup.connect, referenced from method com.example.myexample.MainActivity$MyTask.doInBackground
12-15 11:16:43.033: W/dalvikvm(371): VFY: unable to resolve static method 3462: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection;
12-15 11:16:43.033: D/dalvikvm(371): VFY: replacing opcode 0x71 at 0x0003
12-15 11:16:43.033: D/dalvikvm(371): VFY: dead code 0x0006-000d in Lcom/example/myexample/MainActivity$MyTask;.doInBackground ([Ljava/lang/String;)Ljava/lang/String;
12-15 11:16:43.113: W/dalvikvm(371): threadid=9: thread exiting with uncaught exception (group=0x40015560)
12-15 11:16:43.133: E/AndroidRuntime(371): FATAL EXCEPTION: AsyncTask #1
12-15 11:16:43.133: E/AndroidRuntime(371): java.lang.RuntimeException: An error occured while executing doInBackground()
12-15 11:16:43.133: E/AndroidRuntime(371):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.lang.Thread.run(Thread.java:1019)

与异步连接的致命异常!但我不明白为什么 我的应用程序:

public class MainActivity extends Activity {
MyTask mt;
  TextView tvInfo;
  String URL="http://en.wikipedia.org/";
  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;
      String what1=null;
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      tvInfo.setText("Please wait...");
    }

    protected String doInBackground(String... params) {
      // TimeUnit.SECONDS.sleep(2);
       String url=params[0];
       Document doc;

    try {
        doc = Jsoup.connect(url).get();
        String what1=doc.title();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


      return what1;
    }

    protected void onPostExecute(String result) {
      super.onPostExecute(result);
      tvInfo.setText(result);
    }
  }
}

如果我更改我的代码(没有jsoup)异步工作好:

public class MainActivity extends Activity {
MyTask mt;
  TextView tvInfo;
  String URL="http://en.wikipedia.org/";
  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");
    }

    protected String doInBackground(String... params) {
      // TimeUnit.SECONDS.sleep(2);
       String url=params[0];
       //doc = Jsoup.connect(url).get();
       //String title = doc.title();
      // Log.d("AsyncTask doInBackground","URL: " + params[0]);
      return url;
    }

    protected void onPostExecute(String result) {
      super.onPostExecute(result);
      tvInfo.setText(result);
    }
  }
}
在TextView中,我获得了一个Wiki的URL. 请任何人帮助我这个事实. 谢谢!

p.s我在清单文件中添加了Internet权限 p.p.s我使用jsoup 1.7.1(添加为外部jar文件)

推荐答案

我确实右键单击项目 - >构建路径 - >添加外部档案

请撤消,然后在项目中创建libs/目录(作为src/,res/等),如果您还没有.然后,将JSoup jar放入libs/目录中.这两个都将其添加到您的构建路径和中的内容在APK文件中.

本文地址:https://www.itbaoku.cn/post/102608.html

问题描述

I noticed one interesting thing with my app ( I use Jsoup inside AsyncTask)

After that my app crashed. LogCat is:

 12-15 11:16:43.023: I/dalvikvm(371): Could not find method org.jsoup.Jsoup.connect, referenced from method com.example.myexample.MainActivity$MyTask.doInBackground
12-15 11:16:43.033: W/dalvikvm(371): VFY: unable to resolve static method 3462: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection;
12-15 11:16:43.033: D/dalvikvm(371): VFY: replacing opcode 0x71 at 0x0003
12-15 11:16:43.033: D/dalvikvm(371): VFY: dead code 0x0006-000d in Lcom/example/myexample/MainActivity$MyTask;.doInBackground ([Ljava/lang/String;)Ljava/lang/String;
12-15 11:16:43.113: W/dalvikvm(371): threadid=9: thread exiting with uncaught exception (group=0x40015560)
12-15 11:16:43.133: E/AndroidRuntime(371): FATAL EXCEPTION: AsyncTask #1
12-15 11:16:43.133: E/AndroidRuntime(371): java.lang.RuntimeException: An error occured while executing doInBackground()
12-15 11:16:43.133: E/AndroidRuntime(371):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-15 11:16:43.133: E/AndroidRuntime(371):  at java.lang.Thread.run(Thread.java:1019)

Fatal exception connected with Async! But I don't understand why My App:

public class MainActivity extends Activity {
MyTask mt;
  TextView tvInfo;
  String URL="http://en.wikipedia.org/";
  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;
      String what1=null;
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      tvInfo.setText("Please wait...");
    }

    protected String doInBackground(String... params) {
      // TimeUnit.SECONDS.sleep(2);
       String url=params[0];
       Document doc;

    try {
        doc = Jsoup.connect(url).get();
        String what1=doc.title();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


      return what1;
    }

    protected void onPostExecute(String result) {
      super.onPostExecute(result);
      tvInfo.setText(result);
    }
  }
}

If I change my code (without JSoup) Async works good:

public class MainActivity extends Activity {
MyTask mt;
  TextView tvInfo;
  String URL="http://en.wikipedia.org/";
  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");
    }

    protected String doInBackground(String... params) {
      // TimeUnit.SECONDS.sleep(2);
       String url=params[0];
       //doc = Jsoup.connect(url).get();
       //String title = doc.title();
      // Log.d("AsyncTask doInBackground","URL: " + params[0]);
      return url;
    }

    protected void onPostExecute(String result) {
      super.onPostExecute(result);
      tvInfo.setText(result);
    }
  }
}

In TextView I get a Url of Wiki. Please, can anyone help me with this fact. Thank you!

P.S I add permission of internet in Manifest file P.P.S I use JSoup version 1.7.1 (added as external jar file)

推荐答案

I did so right click on project->build path-> add external archives

Please undo that, then create a libs/ directory in your project (as a peer of src/, res/, etc.) if you do not already have one. Then, put the JSoup JAR in the libs/ directory. This will both add it to your build path and include its contents in your APK file.