My app needs to check for internet access which I successfully implemented. But I have a condition that internet is available but website it is trying to open is currently down. In this case I need to show different message as an output. How can I do so? Please give some idea.
ANDROID Check condition for server down
public boolean isServerReachable()
// To check if server is reachable
{
try {
InetAddress.getByName("google.com").isReachable(3000); //Replace with your name
return true;
} catch (Exception e) {
return false;
}
}
hi guys... i am also working on the same requirement, but in my case the exception occurs as Connection Timed Out: connect. do you guys setup some server (localhost) to run this ? As I am trying without any server configuration... it's simple java program, running by the Netbeans IDE. –
Manchu
I have server having the IP, How can I using this piece of code to check for the server availability. For example: (1.23.25.220:8080) –
Fervor
1.23.25.220:8080 I have a server with the above IP, how can I check the same. –
Fervor
You should check the status of the website's response Like this:
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
and check here to find your status code.
then you can do your job by checking status code like this:
if (status == 200) // sucess
{
also I recommend you to use AsyncTask
for your connection to do communication with server in background.
Try to catch NoHttpResponseException as follow
try{
//code to try to connect to your server
}catch(NoHttpResponseException ex){
//print stacktrace or display some message to say server is down
}
You can use this class. Make object and call methods.
public class ConnectionDetector {
private Context context;
public ConnectionDetector(Context context){
this.context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
public boolean isURLReachable() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL(serverConnection.url); // Insert Url
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(10 * 1000); // 10 s.
urlc.connect();
if (urlc.getResponseCode() == 200) { // 200 = "OK" code (http connection is fine).
Log.wtf("Connection", "Success !");
return true;
} else {
return false;
}
} catch (MalformedURLException e1) {
return false;
} catch (IOException e) {
return false;
}
}
return false;
}
}
No other solution works but your isURLReachable works, great –
Hypethral
© 2022 - 2024 — McMap. All rights reserved.