销毁或关闭HttpClient Android

时间:2011-12-13 21:09:12

标签: java android httpclient http-get

我有一个问题,我认为它可能与HttpClient有关。我正在登录一个网站并使用JSoup从中获取数据。一切正常。这是我的问题,当我想登录其他帐户时,它会显示来自其他帐户的相同数据。只有当我杀死应用程序时,我才能使用不同的凭据登录。我认为我与网站的会话仍然存储在同一个HttpClient中,除非我注销,否则我不会再使用其他帐户登录。解决这个问题的最佳方法是什么?使用HttpGet方法执行注销脚本?或者有没有办法重置HttpCLient。谢谢。代码:

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

        @Override
        public void run() {
            final HttpParams params = new BasicHttpParams();
            HttpClientParams.setRedirecting(params, true);
            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&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",
                        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);

                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();
                        break;
                    }
                    i++;
                    ggg = d.html();

                }

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

        }
    }).start();

}

3 个答案:

答案 0 :(得分:4)

您可以致电httppost.abort();

答案 1 :(得分:1)

我遇到了类似的问题,虽然我在整个应用程序中使用了一个HttpClient实例(Singleton)。因此,在这种情况下,如果您使用抢先身份验证,则只需为应用程序范围的客户端实例执行此操作(例如,如果用户注销):

public void logOut() {
  // keep in mind, 'httpClient' is final and set once in the constructor (Singleton)
  httpClient.getCredentialsProvider().clear();
  httpClient.getCookieStore().clear(); // important
}

另请参阅:HttpClient State Management

答案 2 :(得分:0)

我建议将httpclient的“no-cache”标题设置为false并尝试。不保证解决方案。