我的服务背景:它实现了LocationListener,并在LocationManager实例(locMananager)中注册了更新:
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
在onLocationChanged方法中,它使用当前位置调用名为recordTrace的方法,然后调用getHttpResponse将位置坐标发送到服务器。后一种方法如下:
public InputStream getHttpResponse(String url, ArrayList<NameValuePair> params, int timeout) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
httpClient.setParams(httpParams);
HttpPost post = new HttpPost(url);
if(params != null && !params.isEmpty()) {
try {
post.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "HttpPost.setEntity Error: " + e.getMessage());
lastError = "błąd HTTP";
}
}
CookieStore store = new BasicCookieStore();
if(localCookies != null && localCookies.size() > 0) {
for(int i = 0; i < localCookies.size(); i++) {
store.addCookie(localCookies.get(i));
}
}
HttpContext context = new BasicHttpContext();
context.setAttribute(ClientContext.COOKIE_STORE, store);
HttpResponse response = null;
HttpEntity entity = null;
InputStream content = null;
try {
response = httpClient.execute(post, context);
store = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
List<Cookie> cookies = store.getCookies();
if(cookies != null && cookies.size() > 0) {
localCookies = new ArrayList<BasicClientCookie>();
for(Cookie cookie : cookies) {
localCookies.add((BasicClientCookie) cookie);
}
}
entity = response.getEntity();
content = entity.getContent();
return content;
} catch (ClientProtocolException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw e;
}
}
params是一个带有准备数据的NameValuePair,超时设置为5000(5秒)。 ArrayList localCookies保存在成功登录之前保存的cookie(保留会话)。
问题是:当我松开移动信号(即当我去地铁时)并恢复它时,我得到IOException,除非我重新启动手机,否则无法恢复。
任何帮助将不胜感激。我正在失去理智,秃顶!
谢谢, 彼得。
修改
我做了一些研究。在调用方法getHttpResponse
之后,我利用它返回的InputStream,但毕竟不要关闭它。你可能是那个问题吗?这可能是操作员断开连接然后建立一个新连接,而我的InputStream
以某种方式“保持”前一个连接并产生数据传输问题?
我添加了一个finally
块,现在InputSTream已关闭。但是,由于很难根据需要引起问题(它不会经常发生),我无法检查关闭流是否解决了这个问题。
答案 0 :(得分:1)
经过几天的测试,似乎我找到了解决方案。调用2个方法可以解决问题:
关闭InputStream
httpResponse.getEntity()
执行httpClient.getConnectionManager().shutdown()
完整请求和响应处理的代码段:
String url = "www.someurl.com";
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("login", "mylogin"));
params.add(new BasicNameValuePair("password", "mypassword"));
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream content = httpEntity.getContent();
/*
* utilize content here...
*/
content.close(); // here's the point
httpClient.getConnectionManager().shutdown(); // the second important thing
}
catch (UnsupportedEncodingException e) {}
catch (ClientProtocolException e) {}
catch (IOException e) {}
我正在回答我自己的问题,因为我花了很多时间来寻找造成问题的原因,我想我可以节省一些时间。几天后,应用程序仍然有效,并没有打破连接。
此致
彼得。