HttpClient只会执行一次Android

时间:2011-12-14 01:04:53

标签: java android httpclient http-post http-get

我的HttpClient,HttpPost或HttpGet方法只运行一次。我可以第一次成功登录响应中的get html,但是当我尝试第二次登录时,html是空白的,我甚至没有得到回复。怎么了?

    public void parseDoc() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            sting.loadData("<p></p>", "text/html", null);
            HttpParams params = new BasicHttpParams();
            HttpClientParams.setRedirecting(params, true);
            httpclient = new DefaultHttpClient();

            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&jli=t&jli=t&jli=t&jli=t&jli=t&jli=t&printable=FALSE&portrait_or_landscape=portrait"));
                nameValuePairs.add(new BasicNameValuePair("currDomain",
                        "beardenhs.knoxschools.org"));
                nameValuePairs
                        .add(new BasicNameValuePair("username", user));
                nameValuePairs
                        .add(new BasicNameValuePair("password", pass));
                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();
                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);

                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.getElementsByTag("tbody");
                for (Element d : divs) {
                    if (i == 2) {
                        finishGrades();
                        f++;
                        break;
                    }
                    i++;
                    ggg = d.html();

                }

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

        }
    }).start();

}

1 个答案:

答案 0 :(得分:2)

好吧,我不是一个机器人。从未拥有过智能手机。 (从来没有使用过FB!)所以这不是一个问题,而是一个答案。在这里写问题比在评论部分输入更容易,特别是段落。

您是否有任何理由不关闭或释放http连接?我问,因为我倾向于关闭/释放我的连接。当我正在尝试长轮询服务器时。这是android编程的工作方式吗?

android是单线程还是多线程架构?当有两个线程试图打开连接时会发生什么?第一个线程是否会阻塞第二个线程,尤其是当第一个线程拒绝放弃连接时?

如果您在每个请求的预期结束时关闭或释放连接会怎样?

登录/退出时意味着什么。注销会释放/关闭连接吗?您是否专门注销以便服务器关闭连接或连接是否仍然存在?