My application downloads data in background, if it is available. I am using AsyncTask for that, but when my webservice is not reachable or it takes too long fot the webservice to respond, my application starts working very slow or not working at all.
I am using this piece of code for going online in my AsyncTask:
/* Open connection. */
URL url = new URL(webserviceLink);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
/* Create input stream. */
inputStream = connection.getInputStream();
while ((bytesRead = inputStream.read(buffer, 0, bufferSize)) > 0) {
fileOutputStream.write(buffer, 0, bytesRead);
counter += bytesRead;
}
http://developer.android.com/reference/java/net/URLConnection.html#setConnectTimeout(int)
public void setConnectTimeout (int timeoutMillis)set small connect timeout like this:
connection.setConnectTimeout(5*1000);EDIT: Also you probably would like to set read timeout with this method:
public void setReadTimeout (int timeoutMillis)