Reputation: 641
I have following code to check server availability but it doesn't work I have check internet availability using connectivity manager it works well
public static boolean isConnected(Context context, String url){
ConnectivityManager connectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager!=null){
if ((connectivityManager.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
||(connectivityManager.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
||(connectivityManager.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
||(connectivityManager.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))
{
try{
URL myUrl = new URL(url);
URLConnection connection = myUrl.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
return true;
} catch (Exception e) {
// Handle your exceptions
return false;
}
}
}
return false;
}
Upvotes: 0
Views: 2058
Reputation: 13761
You may simply declare a Socket
. I use the following code on many of my projects and the timeout definitely works.
Socket socket;
final String host = "your.server.IP.or.host";
final int port = 80;
final int timeout = 30000; // 30 seconds
try {
socket = new Socket();
socket.connect(new InetSocketAddress(host, port), timeout);
}
catch (UnknownHostException uhe) {
Log.e("ServerSock", "I couldn't resolve the host you've provided!");
}
catch (SocketTimeoutException ste) {
Log.e("ServerSock", "After a reasonable amount of time, I'm not able to connect, Server is probably down!");
}
catch (IOException ioe) {
Log.e("ServerSock", "Hmmm... Sudden disconnection, probably you should start again!");
}
This might be tricky, though. Precisely on UnknownHostException
s, it may take longer to timeout, about 45 seconds - but on the other side, this usually happens when you cannot resolve the host, so that would morelike mean that your internet access has missconfigured DNS resolution (which is not probable).
Anyway, if you want to hedge your bets, you could simply use your IP address and not a host. This way you'll make sure this is not the problem if any of your exceptions fire.
Upvotes: 0
Reputation: 47807
There are many ways. But i choose this way.You can implement like a below:
HttpClient httpclient = new DefaultHttpClient();
String response = null;
URI url;
try {
String s = "url";
url = new URI(s.replace(" ", "%20"));
Log.e("my webservice", "My webservice : " + url);
HttpGet httpget = new HttpGet(url);
HttpResponse httpResponse = null;
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 = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
// Execute HTTP Post Request
httpResponse = httpClient.execute(httpget);
response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
Log.e("Login service", "resonse length : " + url);
if(response.length()>0){
return true;//server available
}else{
return false;//server not available
}
// this is what we extended for the getting the response string
// which we going to parese for out use in database //
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
Upvotes: 2
Reputation: 5068
you can do :
HttpParams httpParams = new BasicHttpParams();
//int some_reasonable_timeout = (int) (30 * DateUtils.SECOND_IN_MILLIS);
int some_reasonable_timeout = 10;
HttpConnectionParams.setConnectionTimeout(httpParams, some_reasonable_timeout);
HttpConnectionParams.setSoTimeout(httpParams, some_reasonable_timeout);
//DefaultHttpClient client = new DefaultHttpClient();
// get the response
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StatusLine status = response.getStatusLine();
Log.d("tag","status is"+status.toString());
if (status.getStatusCode() == HttpStatus.SC_OK)
{
//cserver is available
}
else
{
//server is not available
}
Upvotes: 1