I\'m writing an application that connects to a webservice and I don\'t want it to wait too long if it can\'t get a connection. I therefore set the connectionTimeout of the h
This method works for me :
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport( endpoint, 3000) ;
With the marked solution I am still getting a UnknownHostException after 30+ seconds. In this case the device is connected to a wifi router but there is no internet access.
The approach taken was to kick off an AsyncTask that will just attempt to resolve the hostname. The blocking call checks every 250 ms to see if it succeeded, and after 4 seconds it will cancel the task and return.
This is what I did to solve it:
private boolean dnsOkay = false;
private static final int DNS_SLEEP_WAIT = 250;
private synchronized boolean resolveDns(){
RemoteDnsCheck check = new RemoteDnsCheck();
check.execute();
try {
int timeSlept = 0;
while(!dnsOkay && timeSlept<4000){
//Log.d("RemoteDnsCheck", "sleeping");
Thread.sleep(DNS_SLEEP_WAIT);
timeSlept+=DNS_SLEEP_WAIT;
//Log.d("RemoteDnsCheck", "slept");
}
} catch (InterruptedException e) {
}
if(!dnsOkay){
Log.d("resolveDns", "cancelling");
check.cancel(true);
Log.d("resolveDns", "cancelled");
}
return dnsOkay;
}
private class RemoteDnsCheck extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
try {
Log.d("RemoteDnsCheck", "starting");
dnsOkay = false;
InetAddress addr = InetAddress.getByName(baseServiceURL);
if(addr!=null){
Log.d("RemoteDnsCheck", "got addr");
dnsOkay = true;
}
} catch (UnknownHostException e) {
Log.d("RemoteDnsCheck", "UnknownHostException");
}
return null;
}
}
Then, any time I want to do a web call, this is called at the beginning of the function:
if(!resolveDns()){
return null;
}
See: https://stackoverflow.com/a/20031077/2609238
The problem might be in the Apache HTTP Client. See HTTPCLIENT-1098. Fixed in 4.1.2.
The timeout exception tries to reverse DNS the IP, for logging purposes. This takes an additional time until the exception is actually fired.
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
Try to do it this way:
HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(envelope,HTTP.UTF_8);
httpPost.setEntity(se);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
BasicHttpResponse httpResponse = (BasicHttpResponse) httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
return entity;
You then can catch a possible ConnectTimeoutException.