我是 android 新手,我正在尝试使用 jsoup 从网站获取数据,但是当我执行 get() 方法时没有任何反应。这是我的代码:
static final String BLOG_URL = "http://xjaphx.wordpress.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_html_parser);
// Show the Up button in the action bar.
setupActionBar();
// process
new DownloadFilesTask().execute();
}
私有类 DownloadFilesTask 扩展 AsyncTask {
String result = "";
@Override
protected String doInBackground(Void... params) {
// get html document structure
Document document=null;
try {
document = Jsoup.connect(BLOG_URL).get();
// selector query
Elements nodeBlogStats = document.select("div#blog-stats ul li");
// check results
if(nodeBlogStats.size() > 0) {
// get value
result = nodeBlogStats.get(0).text();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return
return null;
}
protected void onPostExecute()
{
TextView article = (TextView)findViewById(R.id.textView1);
article.setText(result);
}
}
这是我的 LogCat 日志:
04-21 12:13:24.458: W/System.err(4501): java.net.UnknownHostException: xjaphx.wordpress.com
04-21 12:13:24.458: W/System.err(4501): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
04-21 12:13:24.458: W/System.err(4501): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
04-21 12:13:24.458: W/System.err(4501): at java.net.InetAddress.getAllByName(InetAddress.java:256)
04-21 12:13:24.458: W/System.err(4501): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
04-21 12:13:24.458: W/System.err(4501): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
04-21 12:13:24.458: W/System.err(4501): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
04-21 12:13:24.458: W/System.err(4501): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
04-21 12:13:24.458: W/System.err(4501): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
04-21 12:13:24.458: W/System.err(4501): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
04-21 12:13:24.458: W/System.err(4501): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
04-21 12:13:24.458: W/System.err(4501): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
04-21 12:13:24.458: W/System.err(4501): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
04-21 12:13:24.458: W/System.err(4501): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
04-21 12:13:24.458: W/System.err(4501): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
04-21 12:13:24.458: W/System.err(4501): at pdm.ebudget.HtmlParser$DownloadFilesTask.doInBackground(HtmlParser.java:158)
04-21 12:13:24.458: W/System.err(4501): at pdm.ebudget.HtmlParser$DownloadFilesTask.doInBackground(HtmlParser.java:1)
04-21 12:13:24.468: D/KeyguardViewMediator(1484): setHidden false
04-21 12:13:24.468: W/System.err(4501): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-21 12:13:24.478: W/System.err(4501): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-21 12:13:24.478: W/System.err(4501): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-21 12:13:24.478: W/System.err(4501): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-21 12:13:24.478: W/System.err(4501): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-21 12:13:24.478: W/System.err(4501): at java.lang.Thread.run(Thread.java:1019)
我在清单中加入了 Internet 访问行:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />