JSoup跳过元素Android

时间:2011-12-11 21:27:13

标签: java android httpresponse jsoup http-get

JSoup似乎正在跳过我的HTML字符串中的一些元素。我100%肯定一切都在HTML字符串中,但是当我选择要解析时,JSoup只读取一些元素,或者根本没有。但我知道他们存在。这是我的代码:谢谢:

public void parseDoc() {
    final HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, true);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "https://secure.groupfusion.net/processlogin.php");
    String HTML = "";
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("referral_page",
                "/modules/gradebook/ui/gradebook.phtml?type=student_view"));
        nameValuePairs.add(new BasicNameValuePair("currDomain",
                "beardenhs.knoxschools.org"));
        nameValuePairs.add(new BasicNameValuePair("username", username
                .getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("password", password
                .getText().toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);

        HTML = EntityUtils.toString(response.getEntity());
        Document doc = Jsoup.parse(HTML);
        Element link = doc.select("a").first();
        String linkHref = link.attr("href");
        HttpGet request = new HttpGet();
        try {
            request.setURI(new URI(linkHref));
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response = httpclient.execute(request);
        String html = "";
        InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            str.append(line);
        }
        in.close();
        HTML = str.toString();
        doc = Jsoup.parse(HTML);
        Elements divs = doc.select("div.yuiTop");
        for (Element d: divs) {
            sting.append(d.text());
            sting.append("\n");
        }


    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

}

1 个答案:

答案 0 :(得分:1)

这里有一些奇怪的代码...所以我认为这是一个查询,结果会返回一个超链接列表,你正在屏幕抓取第一个超链接的结果,然后你试图加载第二个链接的内容?那么,你确定服务器正在返回一个有效的超链接吗?尝试在浏览器中加载页面。

如果它有效,那么我不确定问题是什么,但为什么不使用WebView.loadUrl(),让浏览器组件处理它?<​​/ p>