eliangela
eliangela

Reputation: 253

HttpURLConnection.setConnectTimeout is not working

I have an Android app that connects in a server searching for updates. If the server takes a long time to respond (+500ms), I have to finish my method and continue with the program.

I already set the readTimeout and connectTimeout to 500 ms, but even then my method are taking about 30 seconds in this line: c.connect();

This is my code:

HttpURLConnection c = (HttpURLConnection) updateUrl.openConnection();
c.setConnectTimeout(500);
c.setReadTimeout(500);
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();  // the program stops here

What I need to do?

Upvotes: 1

Views: 4692

Answers (1)

eliangela
eliangela

Reputation: 253

Thanks a lot, Asok!! I solved using org.apache.http.client.HttpClient:

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 500);
HttpConnectionParams.setSoTimeout(httpParameters, 500);

HttpGet httpget = new HttpGet(updateUrl.toURI());
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setParams(httpParameters);

HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();

//download file.....

Upvotes: 1

Related Questions