0

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.

arshu
  • 11,805
  • 3
  • 23
  • 21
Manasi
  • 13
  • 1
  • 4
  • I am sending data to server using mobile internet. but if server is down due to some reasons the data packet will be lost. – Manasi Oct 15 '13 at 05:29
  • chekc this http://stackoverflow.com/questions/1443166/android-how-to-check-if-the-server-is-available#comment8345939_1443358 – RobinHood Oct 15 '13 at 05:29
  • I have refered that an using the same line of code. InetAddress.getByName("http://some IP/android/insert.php").isReachable(50000); am i using it in the wrong way. – Manasi Oct 15 '13 at 05:35
  • Try to catch SocketException & ConnectTimeoutException. This would be thrown when your internet is fine but server is down. – arshu Oct 15 '13 at 06:25
  • @Arshad Parwez let me try this. – Manasi Oct 15 '13 at 06:27

3 Answers3

0

After every 3 mins u need to check the internet connetion if available then hit the url or else wait for next 3mins :

public  boolean checkInternetConnection() {

    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        Log.d("Internet Connection  Present","");
        isFound=true;
    } else {
        Log.d("Internet Connection Not Present","");
        isFound= false;
    }
    return isFound;
}
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • this will check internet connection of my mobile phone. I want to check server status. what if my mobile internet works fine but server is down. data is lost in that case – Manasi Oct 15 '13 at 05:27
  • ok then u need to write a server side sript ..which will provide u the acknowledment that server is down or not. if u got the response then u can fire the url or else wait – KOTIOS Oct 15 '13 at 05:29
  • ooh it means if I get reply from server then its up. else its down. rite? But isnt there any way to check server status from mobile? – Manasi Oct 15 '13 at 05:31
  • according to me we need acknowledment from server but dont fire url whose response is heavy i mean just return response code from server scipt that code must identity the status – KOTIOS Oct 15 '13 at 05:33
  • did u got my statement ? – KOTIOS Oct 15 '13 at 05:46
  • yup got it. Actuall there are 200 such mobiles. what code should i write on server side. – Manasi Oct 15 '13 at 05:57
  • u need to write php code for the same and upload it on server and request from ur android device.. this method will support all device since it is backend method ..wont depend on devices .. – KOTIOS Oct 15 '13 at 05:59
0

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.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
0

For checking if Server is down. You can use the following code :

try {
        String response_string = null;
        HttpPost get = new HttpPost(SERVER_URL);
        HttpClient hc = new DefaultHttpClient();
        HttpResponse rp = hc.execute(get);
        response_string = new StringBuffer(EntityUtils.toString(rp.getEntity()));

        Log.d("Json Response  = ", "" + response_string);
        return response_string.toString();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (ConnectTimeoutException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
arshu
  • 11,805
  • 3
  • 23
  • 21