Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android UnknownHostException : is there a way to set timeout?

When I connect to my webservice to retreive data, the phone is sometimes getting disconnected, DNS messed up, etc. Then I get an UnknownHostException which is perfectly fine.

What I want to do is to set a timeout when looking for the hostName here:

 response = httpclient.execute(httpget); 

I have already set :

HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

but they don't seem to apply for HostLookUp. Is there any way to set the timeout at host lookup?

Edit
I just found that the user cannot modify the timeout of nslookup in this post on the hc-dev mailing list.

I will have to manually throw a timeout exception from a timer at that point.

like image 486
weakwire Avatar asked Mar 08 '12 23:03

weakwire


People also ask

What can cause UnknownHostException?

UnknownHostException is a common error message in Java applications. This error typically indicates that there was a DNS resolution failure. If a Java application fails to get a valid DNS answer, then it might throw an UnknownHostException error.

What is UnknownHostException in Android?

java.net.UnknownHostException. Thrown to indicate that the IP address of a host could not be determined.

What causes Java net UnknownHostException?

What Causes UnknownHostException. The UnknownHostException occurs when trying to connect to a remote host using its hostname, but the IP address of the host could not be determined. This usually happens because of a typo in the hostname, or because of a DNS misconfiguration or propagation delay.

How do I fix UnknownHostException?

A few tips to prevent the exception are: Double-check the hostname: Make sure there is no typo, and trim all whitespaces. Check the system's DNS settings: Make sure the DNS server is up and reachable, and if the hostname is new, wait for the DNS server to catch up.


1 Answers

First of all HttpParams httpParams = new BasicHttpParams() is depricated, use this HttpURLConnection conn = (HttpURLConnection) url.openConnection();

when your parameters size greater than 2mb then server give time out responce.

Check your params size and let me know.

like image 86
Gautam Avatar answered Oct 10 '22 16:10

Gautam