0

我正在尝试使用 jsoup 解析网页,并且使用此代码一切正常:

class DownloadSearchResultsTask extends AsyncTask<String, Integer, ArrayList> {
    private String link = "link";
    private String title = "title";
    private String vote = "vote";
    private String age = "age";
    private String size = "size";

    private String seeders = "seeders";
    private String leechers = "leachers";


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub 
        super.onPreExecute();
    }


    @Override
    protected ArrayList doInBackground(String... params) {
        // TODO Auto-generated method stub ArrayList

        <HashMap<String, String>> searchResult = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        String link, title, vote, age, size, seeders, leechers;

        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://www.facebook.com").openConnection();
            Log.d("VIVZ", httpURLConnection.getContentLength() + "");
        }
        catch( MalformedURLException e1 ) {
            // TODO Auto-generated catch block 
            e1.printStackTrace();
        }
        catch( IOException e1 ) {
            // TODO Auto-generated catch block 
            e1.printStackTrace();
        }

        Document mDocument;

        try {
            long l1 = System.nanoTime();
            Log.e("VIVZ", l1 + "");

            mDocument = Jsoup.connect(params[0]).userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").referrer("http://www.google.com").get();

            long l2 = System.nanoTime();
            Log.e("VIVZ", (l2 - l1) + "");
            Elements mResults = mDocument.select("div.results dl");

            for( Element result : mResults ) {
                map = new HashMap<String, String>();
                Elements elements = result.select("dt a");

                for( Element linkAndTitle : elements ) {

                    link = linkAndTitle.attr("abs:href");
                    title = linkAndTitle.text();
                    map.put(this.link, link);
                    map.put(this.title, title);
                }

                elements = result.select("dd span.v");

                for( Element v : elements ) {
                    vote = v.text();
                    map.put(this.vote, vote);
                }

                elements = result.select("dd span.a");

                for( Element a : elements ) {
                    age = a.text();
                    map.put(this.age, age);
                }

                elements = result.select("dd span.s");

                for( Element s : elements ) {
                    size = s.text();
                    map.put(this.size, size);
                }

                elements = result.select("dd span.u");

                for( Element u : elements ) {
                    seeders = u.text();
                    map.put(this.seeders, seeders);
                }

                elements = result.select("dd span.d");

                for( Element d : elements ) {
                    leechers = d.text();
                    map.put(this.leechers, leechers);
                }

                searchResult.add(map);

            }
            Log.e("VIVZ", searchResult.toString());

            return searchResult;
        }
        catch( IOException e ) {
            // TODO Auto-generated catch block 
            Log.e("VIVZ", e + "");

        }
        return null;
    }


    @Override
    protected void onPostExecute(ArrayList result) {
        // TODO Auto-generated method stub 
        super.onPostExecute(result);
    }
}

问题是我想在解析它之前获取页面的大小并显示一个确定的进度条请帮助我.....

提前谢谢

4

1 回答 1

0

读取 http 响应的 Content-Length 标头。

它实际上可以直接使用 Jsoup,如下所示:

Connection conn = Jsoup.connect(url);
conn.userAgent("xxx");
Connection.Response response = conn.execute();
int length = response.header("Content-Length");
Document doc = response.parse();
于 2013-10-19T04:16:55.057 回答