Android发布连接SingleClientConnManager登录

时间:2010-11-17 03:20:51

标签: android login connection

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

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):确保在分配另一个连接之前释放连接。

如何释放连接?

1 个答案:

答案 0 :(得分:0)

您的代码正在进行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() ;

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