java 安卓 HTML抓取与Jsoup崩溃
我试图编写一个简单的应用程序,使用Jsoup将TextView更改为Google Html上的标题。但当我运行它时,应用程序崩溃了。有人知道为什么吗? 我认为Asynk任务可能有问题,有人能看一下代码并告诉我错误是什么吗
import java.io.IOException;
import 安卓.app.Activity;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.widget.TextView;
import 安卓.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
非常感谢您的回答
# 1 楼答案
在android清单文件中添加以下权限,应用程序不会崩溃