【发布时间】:2014-07-03 16:07:21
【问题描述】:
我尝试编写一个简单的应用程序,使用 Jsoup 将 TextView 更改为 Google Html 上的标题。但是当我运行它时,应用程序崩溃了。有人知道为什么吗? 我认为 Asynk 任务可能有问题,有人可以看看代码并告诉我错误是什么吗?
import java.io.IOException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class HttpExample extends Activity{
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
tvResult = (TextView)findViewById(R.id.tvHttp);
new Title().execute();
}
private class Title extends AsyncTask <Void,Void,Void>{
String title;
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
// Connect to the web site
Document document = Jsoup.connect("http://www.google.com").get();
// Get the html document title
title = document.title();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}//doInBackground()
protected void onPostExecute(Void result) {
// Set title into TextView
tvResult.setText(title);
}//onPostExecute()
}//Title class
}//Activity
非常感谢您的回答...
【问题讨论】:
-
我试过了,没有崩溃。您是否添加了访问 Internet 的权限?抛出什么样的异常?
标签: java android json android-asynctask web-scraping