Android 释放连接 SingleClientConnManager 登录

发布于 2024-10-03 05:47:15 字数 2388 浏览 0 评论 0原文

我编写了这段代码用于登录我的网站并检查我是否已登录。

public class smerdLearning extends Activity {
/** Called when the activity is first created. */
final HttpParams params = new BasicHttpParams();
final HttpClient client = new DefaultHttpClient(params);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String url, user, pwd, user_field, pwd_field;

    url = "http://SMERDER.org/login/";


    user_field = "username";
    pwd_field = "password";
    user = "robert";
    pwd = "fanello";

    final List<NameValuePair> myList = new ArrayList<NameValuePair>(2);
    myList.add(new BasicNameValuePair(user_field, user)); 
    myList.add(new BasicNameValuePair(pwd_field, pwd));


    final HttpPost post = new HttpPost(url);

    Button login_button = (Button)findViewById(R.id.login_button);

    login_button.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            try {
                post.setEntity(new UrlEncodedFormEntity(myList));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            try {
                HttpResponse response = client.execute(post);
                System.out.println("Am I logged in?");
                String am_i_logged= "http://smerder.smerder.org/api/am-i-logged-in/";

                try {
                    URLreader(am_i_logged, response);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }
    });
}

void URLreader(String url, HttpResponse response) throws Exception{
    response = client.execute(new HttpGet(url));
    HttpEntity entity = response.getEntity();

    BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));

    String inputLine;

    while ((inputLine = in.readLine()) != null){
        System.out.println(inputLine);
    }
    in.close();
}

但是

,如果我使用 LogCat 查看日志,我会看到 2 个警告: 11-17 03:07:40.976: WARN/SingleClientConnManager(1195):确保在分配另一个连接之前释放连接。

我怎样才能释放连接?

I wrote this code for log into my website and check if I'm logged or not.

public class smerdLearning extends Activity {
/** Called when the activity is first created. */
final HttpParams params = new BasicHttpParams();
final HttpClient client = new DefaultHttpClient(params);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String url, user, pwd, user_field, pwd_field;

    url = "http://SMERDER.org/login/";


    user_field = "username";
    pwd_field = "password";
    user = "robert";
    pwd = "fanello";

    final List<NameValuePair> myList = new ArrayList<NameValuePair>(2);
    myList.add(new BasicNameValuePair(user_field, user)); 
    myList.add(new BasicNameValuePair(pwd_field, pwd));


    final HttpPost post = new HttpPost(url);

    Button login_button = (Button)findViewById(R.id.login_button);

    login_button.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            try {
                post.setEntity(new UrlEncodedFormEntity(myList));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            try {
                HttpResponse response = client.execute(post);
                System.out.println("Am I logged in?");
                String am_i_logged= "http://smerder.smerder.org/api/am-i-logged-in/";

                try {
                    URLreader(am_i_logged, response);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }
    });
}

void URLreader(String url, HttpResponse response) throws Exception{
    response = client.execute(new HttpGet(url));
    HttpEntity entity = response.getEntity();

    BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));

    String inputLine;

    while ((inputLine = in.readLine()) != null){
        System.out.println(inputLine);
    }
    in.close();
}

}

But, if I see the log with LogCat, I see 2 warning:
11-17 03:07:40.976: WARN/SingleClientConnManager(1195): Make sure to release the connection before allocating another one.

How can I release the connection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2024-10-10 05:47:15

您的代码正在执行 2 个 http 操作。

在 URLreader 调用之前,您有一个

HttpResponse response = client.execute(post);

对 URLreader 的调用,

void URLreader(String url, HttpResponse response) throws Exception{
    response = client.execute(new HttpGet(url));
    HttpEntity entity = response.getEntity();

    BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));

    String inputLine;

    while ((inputLine = in.readLine()) != null){
        System.out.println(inputLine);
    }
    in.close();
}

您将丢弃传递的响应,并且没有人在其流上调用 close 。在某个地方(URLreader 之前或之后),您

InputStream is = post.getEntity().getContent()
if(is!=null)
  is.close()

可能还想调用

post.getEntity().consumeContent() ;

我没有,但在示例代码中看到了它。

Your code is doing 2 http operations.

prior to the URLreader call, you have a

HttpResponse response = client.execute(post);

then a call to URLreader

void URLreader(String url, HttpResponse response) throws Exception{
    response = client.execute(new HttpGet(url));
    HttpEntity entity = response.getEntity();

    BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));

    String inputLine;

    while ((inputLine = in.readLine()) != null){
        System.out.println(inputLine);
    }
    in.close();
}

You're throwing away the passed in response, and no on is calling close on it's stream. Somewhere(pre or post URLreader), you need to

InputStream is = post.getEntity().getContent()
if(is!=null)
  is.close()

You might also want to throw in a call to

post.getEntity().consumeContent() ;

I don't have that, but saw it in example code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文