在解析之前获取网页的大小

时间:2013-10-19 03:50:09

标签: android jsoup

我正在尝试使用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);
    }
}

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

提前

thanx

1 个答案:

答案 0 :(得分: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();