长轮询(comet)上的HttpURLConnection超时 - android

问题描述 投票:0回答:1

我正在尝试从 Comet URL 链接检索 json 字符串。

这里是API链接:http://www.plurk.com/API#realtime 描述如下:

您将从 /APP/Realtime/getUserChannel 获取一个 URL,然后向该 URL 发出 GET 请求以获取新数据。如果您的频道中没有添加新数据,您的请求将休眠约 50 秒,然后再返回响应。您不会收到有关登录用户添加的响应的通知,但您会收到有关新 plurk 的通知。

我能够获取 comet_server url 并将其粘贴到 Firefox 并手动获取结果。然而,当我尝试在android中获取这些json字符串时,我只得到了超时错误。

01:48:51.698 com.net.xerothermic.plurk 信息 PLURK http://comet58.plurk.com:80/comet?channel=...&offset=0

01:53:43.680 com.net.xerothermic.plurk 错误 PLURK HTTP con。得到 响应错误:连接超时

这是我用来检索数据的代码。

    URL url = new URL(urlString);
    HttpURLConnection conn = null;
    try
    {
        conn = (HttpURLConnection) url.openConnection();
    }
    catch (IOException ex)
    {
        Log.e("PLURK", "HTTP con. open error:" + ex.getMessage());
        return "";
    }
    try
    {
        conn.setRequestMethod("GET");
    }
    catch (ProtocolException ex)
    {
        Log.e("PLURK", "HTTP con. set method error:" + ex.getMessage());
    }
    try
    {
        return conn.getResponseMessage();
    }
    catch (IOException ex)
    {
        Log.e("PLURK", "HTTP con. get response error:" + ex.getMessage());
        return "";
    }

任何建议都非常感激!

编辑:这是浏览器的输出。我是否错过设置某些属性? enter image description here

java android httpurlconnection comet
1个回答
0
投票

即使超时值默认设置为 0(意味着无限等待),我发现我仍然需要显式设置超时值,以免引发 IOException。

setConnectTimeout(70000);
setReadTimeout(70000);

这仅在 Android 上需要,但在 Windows 上不需要...

© www.soinside.com 2019 - 2024. All rights reserved.