I know you have tried to ping the server and got the response code as 200 that's for a okay situation but when you get the response as 200, it may depend on various factors.
Reference
: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Now, what I believe is that if the data packets
are sent back to the server and if you do get the packets in the response, there is a connection success.
However, you may want to wait for the server to respond or set some connection
or read timeout
to make sure that there is no bad response.
This is a sample code I have used in the past to determine the internet connectivity. I am using a Process
to determine the check and tried to get the return value as 0
.
public Boolean isConnectionAvailable() {
try {
Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
int returnVal = process.waitFor();
boolean reachable = (returnVal == 0);
if (reachable) {
Log.i(TAG, "Connection Successful");
return reachable;
} else {
Log.e(TAG, "No Internet access");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
process.destroy();
}
return false;
}
In the above code, I have google server as a medium for the check. However, you can use 8.8.8.8 as well like this :
Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 8.8.8.8");
When the process executes, it will give a return value and that will determine the connection failure and success.
The key element is use the waitFor()
method from the java class Process.class
that causes the calling thread to wait for the native process associated with this object to finish executing.
.
Reference : http://developer.android.com/reference/java/lang/Process.html#waitFor%28%29
Give it a shot and I will be happy if this helps. If there's a better answer, I am glad to accept it.. Thanks .. :)