Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpURLConnection, problem turning on/off WiFi and trying to connect
    text
    copied!<p>I have an application which is a RESTful API consumer, and I wish to have a timeout on my connection with the API. </p> <p>As I've searched, and also tested, the HttpURLConnection.setReadTimeout() method doesn't work, so the solution I found was to use an AsyncTask which will try to connect to the server and then pass the timeout to the AsyncTask.get(). </p> <p>It works, partially. The problem is when I do the following:</p> <ul> <li>Enter the application with the WiFi turned on. I click "Login" button and get "Invalid/User password". Ok.</li> <li>Turn off the WiFi, click "Login" button. The application tries to connect but after 5 seconds (the timeout I chose) it shows me the notification dialog saying I'm not connected. Ok, everything as expected.</li> <li>Turn on back the WiFi, click "Login" button. It stills working as if I was not connected, always showing the dialog. I can wait many seconds but the behaviour is the same as If I was disconnected.</li> </ul> <p>I debugged all my code, step by step using Eclipse and there's nothing wrong with the logic. My HttpURLConnection is always a new object, so I'm not trying to use the same connection object to connect after the WiFi is turned on back... Also I'm using the <a href="https://github.com/fernandezpablo85/scribe-java" rel="nofollow">Scribe</a> library, for OAuth, but I checked the source code and everything seems ok, except that I changed the method which creates a connection to always use a new instance.</p> <p>I'm starting to think that Android is "caching" my connection object or the AsyncTask object... </p> <p>Below some code of my RequestManager class:</p> <pre><code>public class RequestManager { private static RequestManager self = new RequestManager(); private Request currentRequest; private static final long TIMEOUT = 5000; public static RequestManager getInstance() { return self; } public void startRequest(Request request) { if (currentRequest != null) return; currentRequest = request; } public String getResponse() throws ConnectionErrorException { RequestThreat rt = new RequestThread(); rt.execute(currentRequest); try { return (String) rt.get(TIMEOUT, TimeUnit.MILISECONDS); } catch (InterruptedException e) { } catch (ExecutionException e) { } catch (TimeoutException e) { throw new ConnectionErrorException(); } finally { endRequest(); } return null; } public void endRequest() { currentRequest = null; } private class RequestThread extends AsyncTask&lt;Request, Integer, String&gt; { @Override protected String doInBackground(Request... requestParams) { return requestParams[0].send().getBody(); } } } </code></pre> <p>Also in the method I call <code>getResponse()</code> I'm calling <code>endRequest()</code> after.</p> <p>Any thoughts?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload