JSoup doesn't work with AsyncTask

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

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)

回答1:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!