Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpURLConnection setConnectTimeout() has no effect

Tags:

java

android

I'm connecting to a simple RSS feed using HTTPUrlConnection. It works perfectly. I'd like to add a timeout to the connection since I don't want my app hanging in the event of a bad connection or whatever. This is the code I use and the setConnectTimeout method doesn't have any effect whatsoever.

        HttpURLConnection http = (HttpURLConnection) mURL.openConnection();         http.setConnectTimeout(15000); //timeout after 15 seconds ... 

If it helps I'm developing on android.

like image 726
Jon Snow Avatar asked Jul 26 '11 12:07

Jon Snow


People also ask

What is setConnectTimeout in Java?

Connection timeout means response timeout of any JDBC call invoking data transmission over a connection socket. If the response message is not received within the time specified, an I/O exception is thrown. The JDBC standard (2.0/3.0) does not support setting of the connection timeout.

What is setConnectTimeout?

setConnectTimeout. public void setConnectTimeout(int timeout) Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java. net.

What is meant by URL connection?

URLConnection is an abstract class whose subclasses form the link between the user application and any resource on the web. We can use it to read/write from/to any resource referenced by a URL object. There are mainly two subclasses that extend the URLConnection class.


2 Answers

You should try to set the read timeout as well (http.setReadTimeout()). Oftentimes, a web server will happily accept your connection, but it might be slow in actually responding to the request.

like image 181
pap Avatar answered Sep 22 '22 02:09

pap


You probably either/both:
1) Don't read anything from connection
2) Don't catch & handle the exception properly

As mentioned here, use logic similar to this:

int TIMEOUT_VALUE = 1000; try {     URL testUrl = new URL("http://google.com");     StringBuilder answer = new StringBuilder(100000);      long start = System.nanoTime();      URLConnection testConnection = testUrl.openConnection();     testConnection.setConnectTimeout(TIMEOUT_VALUE);     testConnection.setReadTimeout(TIMEOUT_VALUE);     BufferedReader in = new BufferedReader(new InputStreamReader(testConnection.getInputStream()));     String inputLine;      while ((inputLine = in.readLine()) != null) {         answer.append(inputLine);         answer.append("\n");     }     in.close();      long elapsed = System.nanoTime() - start;     System.out.println("Elapsed (ms): " + elapsed / 1000000);     System.out.println("Answer:");     System.out.println(answer); } catch (SocketTimeoutException e) {     System.out.println("More than " + TIMEOUT_VALUE + " elapsed."); } 
like image 20
Caner Avatar answered Sep 22 '22 02:09

Caner