So I'm running into a not working socket timeout. I followed all instructions given by existing posts, but its still not working (I never get a socket timeout exception). Here is my code:
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String location = params[0];
try {
HttpGet httpGet = new HttpGet(location);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 0;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
HttpResponse response = client.execute(httpGet);
Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
return new Scanner(out.toString()).useDelimiter("\\A").next();
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String result) {
try {
// doStuff
} catch (Exception e) {
e.printStackTrace();
}
}
};
task.execute(location);
So I should get a socket timeout exception after two seconds, but the client is just ignoring that. Any help?
Thanks in advance
So here is all of the code:
public void fetch(String location) {
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String location = params[0];
try {
HttpGet httpGet = new HttpGet(location);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 0;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
HttpResponse response = client.execute(httpGet);
Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
return new Scanner(out.toString()).useDelimiter("\\A").next();
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String result) {
try {
//doStuff
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
task.execute(location);
}
A while back I had similar problems. I've experimented a bit and noticed that when I ran my app in the emulator the timeouts didn't work and when I ran it on a real device it did work. I've tested it with the
DefaultHttpClient
,HttpUrlConnection
andAndroidHttpClient
, all three showed the same results; an IOexception (UnknownHostException) after about 20 seconds in the emulator regardless of any set timeout.Googling revealed that other people have also reported problems with timeout:
None of the proposed solutions worked for me, so I guess the only reliable solution is to manage timeouts yourself.