Android: How to check if the server is available?
He probably needs Java code since he's working on Android. The Java equivalent -- which I believe works on Android -- should be:
InetAddress.getByName(host).isReachable(timeOut)
server availability checker in Android
After checking the internet connectivity, you can simply make a test API hit to check server status. Send the request to server if server respond ok, start next work or in-other case handle server not responding message for user.
Here is a sample using okhttp
public boolean isServerUp(){
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("your server url here")
.build();
client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
e.printStackTrace();
return false;
}
@Override public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
return true;
}
});
}
return false;
}
how to check if server is down in android
You need to send data again after every 3 minutes, You can't actually control if server is responding or not, or your device's network performance. But you can control the calls you are making to the server. If you are not getting data acknowledgement it means your data is not yet received by the server and you need to send it again. Just check the acknowledgement of data receive from server and then send/resend data.
Checking Host Reachability/Availability in Android
It's not pretty but this is how I did it:
boolean exists = false;
try {
SocketAddress sockaddr = new InetSocketAddress(ip, port);
// Create an unbound socket
Socket sock = new Socket();
// This method will block no more than timeoutMs.
// If the timeout occurs, SocketTimeoutException is thrown.
int timeoutMs = 2000; // 2 seconds
sock.connect(sockaddr, timeoutMs);
exists = true;
} catch(IOException e) {
// Handle exception
}
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;
}
}
How to check server availability in Android?
You can create a servlet/webpages or whatever you feel comfortable on the server that return ok or error depending on server status from your android you must call that url and check returned value, also on your try/catch you must watch specific exception for example related with timeout, also check for http status if your http request returned 200 then was ok and you can fetch server status from that url
Check http://developer.android.com/reference/java/net/HttpURLConnection.html
boolean isOK = false;
try {
URL url = new URL("http://yourserverurl/yourstatusmethod");
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.connect();
if (urlcon.getResponseCode() == 200) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
String serverStatus = readStream(in); //assuming that "http://yourserverurl/yourstatusmethod" returns OK or ERROR depending on your server status check
isOK = (serverStatus.equalsIgnoreCase("OK"));
}else{
isOK = false;
}
url.disconnect();
} catch (MalformedURLException e1) {
isOK = false;
e1.printStackTrace();
} catch (IOException e) {
isOK = false;
e.printStackTrace();
}
the readStream is a method that convert inputstream to string
public static String readStream (InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
this is just and idea.... there are a lot of ways to check server availability
Related Topics
How to Animate Addition or Removal of Android Listview Rows
Detect 7 Inch and 10 Inch Tablet Programmatically
Android + Pair Devices via Bluetooth Programmatically
Adding Items to Endless Scroll Recyclerview with Progressbar at Bottom
Android Open External Storage Directory(Sdcard) for Storing File
How to Move Bluetooth Activity into a Service
Open Soft Keyboard Programmatically
Appcompatactivity.Oncreate Can Only Be Called from Within the Same Library Group
Android Share Images from Assets Folder
Pass Arraylist of User Defined Objects to Intent Android
Can't Stop the Ringing Alarm from Another Activity
Adb - Android - Getting the Name of the Current Activity
How to Use View Pager with Views (Not with Fragments)
Implementing a Rich Text Editor in Android
Why Do Most Fields (Class Members) in Android Tutorial Start with 'M'