1
我在某些客户的手机上遇到了一个奇怪的问题。看起来,在离开服务区几个小时之后,我的Android应用程序将失去访问网络的能力。其他应用程序(如Web浏览器或电子邮件)将能够访问Web,但不能访问我的应用程序。Android应用无法通过网络进行通信
唯一可以想象的解释是,当没有数据服务时,它以某种方式泄漏套接字。
这里是我的代码:
String sendWebPOST(String url, String pPostData) throws IOException {
AndroidHttpClient c = null;
InputStream is = null;
OutputStream os = null;
int rc;
String strResponse = null;
try {
c = AndroidHttpClient.newInstance("Mobile");
// Set the request method and headers
HttpPost request = new HttpPost(url);
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
request.setEntity(new StringEntity(pPostData));
try {
HttpResponse response = c.execute(request);
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = response.getStatusLine().getStatusCode();
if (rc != HttpStatus.SC_OK) {
throw new IOException("HTTP response code: " + rc);
}
try {
is = response.getEntity().getContent();
int ch;
StringBuffer sb = new StringBuffer();
while ((ch = is.read()) != -1) {
sb.append((char) ch);
}
//if(sb.length() > 0)
strResponse = sb.toString();
}
finally {
if (is != null) {
is.close();
}
}
}
finally {
if (os != null) {
os.close();
}
}
}
catch (ClassCastException e) {
throw new IllegalArgumentException("Not an HTTP URL");
}
finally {
if (c != null) {
c.close();
}
}
return strResponse;
}
这个函数被调用大约每隔十分钟更新发送到远程服务器。当应用程序进入这种怪异状态时,用户仍然能够打开活动,与菜单进行交互等,以便应用程序仍在运行。但是,它无法通过网络发送任何内容。
任何想法可能会发生什么?
有问题的手机是在T-Mobile网络上运行Android 2.3的myTouch 4G和三星Galaxy II。
谢谢。