我正在研究jsoup库,我已经编写了这个简单的代码来观察它是如何工作的: 我有以下代码,其中我尝试连接到google.com并解析该网站上的文字:
我的代码:
public class MainActivity extends Activity {
ProgressDialog _dialog;
String title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_dialog = new ProgressDialog(this);
_dialog.setMessage("Jsoup parsing...");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
new TheTask().execute();
}
@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;
}
class TheTask extends AsyncTask<Void,String,String>
{
@Override
protected String doInBackground(Void... arg0) {
// TODO Auto-generated method stub
Document doc;
try {
doc = Jsoup.connect("http://google.com/").get();
title = doc.title();
}
catch(Exception e)
{
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
_dialog.dismiss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
_dialog.show();
}
}
}
错误:
06-21 20:26:38.487: E/AndroidRuntime(1766): FATAL EXCEPTION: AsyncTask #1
06-21 20:26:38.487: E/AndroidRuntime(1766): Process: com.mmexample.newschanr, PID: 1766
06-21 20:26:38.487: E/AndroidRuntime(1766): java.lang.RuntimeException: An error occured while executing doInBackground()
06-21 20:26:38.487: E/AndroidRuntime(1766): at android.os.AsyncTask$3.done(AsyncTask.java:300)
06-21 20:26:38.487: E/AndroidRuntime(1766): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-21 20:26:38.487: E/AndroidRuntime(1766): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-21 20:26:38.487: E/AndroidRuntime(1766): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-21 20:26:38.487: E/AndroidRuntime(1766): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-21 20:26:38.487: E/AndroidRuntime(1766): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-21 20:26:38.487: E/AndroidRuntime(1766): at java.lang.Thread.run(Thread.java:841)
06-21 20:26:38.487: E/AndroidRuntime(1766): Caused by: java.lang.NoClassDefFoundError: org.jsoup.Jsoup
06-21 20:26:38.487: E/AndroidRuntime(1766): at com.mmexample.newschanr.MainActivity$TheTask.doInBackground(MainActivity.java:74)
06-21 20:26:38.487: E/AndroidRuntime(1766): at com.mmexample.newschanr.MainActivity$TheTask.doInBackground(MainActivity.java:1)
06-21 20:26:38.487: E/AndroidRuntime(1766): at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-21 20:26:38.487: E/AndroidRuntime(1766): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-21 20:26:38.487: E/AndroidRuntime(1766): ... 3 more