javaandroidhttpurlconnectioncomet

HttpURLConnection timeout on long polling (comet) - android


I'm trying to retrieve a json string from a comet URL link.

Here is the API link: http://www.plurk.com/API#realtime Here is the description:

You'll get an URL from /APP/Realtime/getUserChannel and you do GET requests to this URL to get new data. Your request will sleep for about 50 seconds before returning a response if there is no new data added to your channel. You won't get notifications on responses that the logged in user adds, but you will get notifications for new plurks.

I was able to obtain the comet_server url and paste that to firefox and get the result manually. However, when I tried to get these json string in android, I only got timeout error.

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

01:53:43.680 com.net.xerothermic.plurk ERROR PLURK HTTP con. get response error:Connection timed out

Here is the code I used to retrieve the data.

    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 "";
    }

Any suggestion is much appreciated!

EDIT: here is the output from a browser. Did I miss to set some properties? enter image description here


Solution

  • Even though the timeout value was set to 0 by default (meaning wait infinitely), I found I still need to explicit set the timeout value in order to not raise IOException.

    setConnectTimeout(70000);
    setReadTimeout(70000);
    

    This is only needed on android but not Windows...