有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android https长轮询:轮询间隔超过70秒时无法接收响应

我正在尝试在我的安卓应用程序上实现一个长轮询推送服务(基于Microsofts AUTD ActiveSync->;http://technet.microsoft.com/en-us/library/aa997252.aspx

我已经实现了在Tomcat7.0上运行Servlets 3.0的服务器部分。我已经用CURL测试了我的AsyncServlet,一切正常

这就是我测试AsyncServlet的方法:

curl "https://webservice.mydomain.net/push?id=1&heartbeat=180000" --insecure --user user:password -D -

3分钟后的服务器响应(心跳间隔)

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 01:00:00 CET
Content-Type: application/json;charset=ISO-8859-1
Content-Length: 0
Date: Sat, 15 Jan 2011 23:38:57 GMT

在我的安卓应用程序上,我使用了以下代码:

HttpParams httpParams = mDefaultHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, mHeartbeat + TIMEOUT_TOLERANCE);
mDefaultHttpClient.setParams(httpParams);

HttpGet httpGet = new HttpGet(mPushURL);
httpGet.addHeader("Accept", "application/json");

try {
    Log.i(TAG, "Executing GET(PUSH) request " + httpGet.getRequestLine());
    HttpResponse httpResponse = mDefaultHttpClient.execute(httpGet);
    Log.i(TAG, httpResponse.getStatusLine().toString());
    Log.i(TAG, convertInputStream(httpResponse.getEntity().getContent())); //For testing purposes
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

我做了几次测试。如果我使用70秒的心跳间隔,我会收到响应。如果我使用>;70秒心跳间隔我收到(在指定的心跳间隔之后)SocketTimeoutException而不是HTTP 200 OK响应。(澄清:我的set SocketTimeout(SoTimeout)工作正常,但可能连接丢失,我不知道原因)

谢谢你的回复


共 (2) 个答案

  1. # 1 楼答案

    尝试添加:

    ConnManagerParams.setTimeout(params, CONNECTION_TIMEOUT);
    
  2. # 2 楼答案

    使用URLConnection并设置readTimeOut。购买这很难说。我认为android网络层正在冻结开放的连接。(我正在从事类似的项目。找不到为什么开放的连接会冻结)