无法使用Jsoup android连接到网站

时间:2013-04-21 09:11:02

标签: android web jsoup

我是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)

我已将我的清单放入互联网访问线:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" /> 

1 个答案:

答案 0 :(得分:1)

我的不好,我的Android终端上没有在线连接^^,对不起!