Android 释放连接 SingleClientConnManager 登录
我编写了这段代码用于登录我的网站并检查我是否已登录。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码正在执行 2 个 http 操作。
在 URLreader 调用之前,您有一个
对 URLreader 的调用,
您将丢弃传递的响应,并且没有人在其流上调用 close 。在某个地方(URLreader 之前或之后),您
可能还想调用
我没有,但在示例代码中看到了它。
Your code is doing 2 http operations.
prior to the URLreader call, you have a
then a call to URLreader
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
You might also want to throw in a call to
I don't have that, but saw it in example code.