私はアンドロイドが初めてで、jsoup を使用して Web サイトからデータを取得しようとしていますが、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 accesses 行を入れました。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />