I want to send a string to a server at every 3 min. I am sending data from android phone to server using following code :
String stringDatatoSend="Hii server";
HttpEntity entity;
HttpClient client = new DefaultHttpClient();
String url ="http://some IP/android/insert.php";
HttpPost request = new HttpPost(url);
StringEntity se = new StringEntity(stringDatatoSend);
se.setContentEncoding("UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
entity = se;
request.setEntity(entity);
HttpResponse response = client.execute(request);
entity = response.getEntity();
But if the server is down data is lost because data is send irrespective of any acknowledgement. how can I check if
the server is alive before the data is send to avoid data loss.I have tried following code but it always returns false.
public boolean isConnectedToServer() {
try {
if(InetAddress.getByName("http://some IP/android/insert.php").isReachable(50000)) {
return true;
} else {
return false;
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
}
I have also got these two options :
netAddress address = InetAddress.getByName(HOST_NAME);
boolean reachable = address.isReachable(timeout);
and by using runtime:
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("ping www.google.com");
what should be the host name ip or address.