1

I am trying to implement ping using HttpGet but behavior is random.

I am having following code which test the internet/server connectivity:

boolean result = false;
HttpGet request = new HttpGet("www.MyServer.com");
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpClient = new DefaultHttpClient(httpParameters);

try
{
    HttpConnectionParams.setConnectionTimeout(httpParameters, 6000);
    HttpConnectionParams.setSoTimeout(httpParameters, 6000); 
    HttpResponse response = httpClient.execute(request);

    int status = response.getStatusLine().getStatusCode();

    if (status == HttpStatus.SC_OK) 
    {
        result = true;
    }

}
catch(Exception e)
{
    e.printStackTrace();
    result = false;
}

Log.d("App", "Ping Result:"+result);

Above code I am running in thread as it may take time. When I run this test for first time then I get result as true, but then after behavior is random, some times it given me error 'host unreachable' and I get result as false.

I just want to test is server is reachable from the currently configured Android network.

Is there any reliable API to test the internet/server connectivity?

UPDATE:

In a Service i have following function which initiates the test.

void startTest()
{ 
    ServerTestThread mServerTestThread = new ServerTestThread()
    mServerTestThread.start();
}

class ServerTestThread extends Thread 
{
    boolean result = false;
    public void run() 
    {   
         //HttpGet code

                 //Send Message TO GUI Thread with result.
    }
}

Above startTest function is creating instance of the test thread and calling start function. when test is done I am sending message to main thread which contains the result.

Thanks.

User7723337
  • 11,857
  • 27
  • 101
  • 182

1 Answers1

2

There is no problem with your code. That means either:

  1. Sometimes server is really unreachable
  2. Connection is slow and it time outs before server is reached.

So test setting timeout to some larger value, such as 60000(60sec), and check again. If it works, then you know it was because of timeout.

EDIT

Also please make this change, maybe it gives us more info:

Log.d("App", "Status:" + status);
if (status == HttpStatus.SC_OK) 
{
    result = true;
}

EDIT2

class ServerTestThread extends Thread 
{

    public static boolean result = false;
    public static HttpGet request = new HttpGet("www.MyServer.com");
    public static HttpParams httpParameters = new BasicHttpParams();
    public static HttpClient httpClient = new DefaultHttpClient(httpParameters);

    boolean result = false;
    public void run() 
    {   
         //HttpGet code

                 //Send Message TO GUI Thread with result.
    }

}

As a bonus, this will tell if you if you're connected to a network.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Caner
  • 57,267
  • 35
  • 174
  • 180
  • Thanks for the reply, but why it works for first time. if time out is less then it should fail on first time also. Do i need to add some kind of clean up as this code is getting called in a thread. – User7723337 Mar 16 '12 at 09:02
  • ok, when you want to run for the 2nd time what do you do? do you call `startTest()` again? – Caner Mar 16 '12 at 09:37
  • yes i call startTest() again. and that time my test fails and also in every 2nd run it gives me Exception. – User7723337 Mar 16 '12 at 09:51
  • ok, that means that you create a new thread and new connection is made each time you call `startTest`. Maybe if we change it so that only 1 connection is made it works. Change `ServerTestThread` as in my edited answer and comment out previous `result, request,httpParameters,httpClient` definitions. – Caner Mar 16 '12 at 10:00
  • Thing is that i am using it just for checking if i have connection to server/internet and my android network keeps changing from Ethernet/WIFI/Mobile so i need to check every time after changing the network if we are connected to internet/server. So as you have suggested maintaining single instance of 'ServerTestThread' through-out and calling run function with only code 'httpClient.execute(request)' and `int status = response.getStatusLine().getStatusCode();` this is what you are suggesting. will this change will send the request using currently connected network type. – User7723337 Mar 16 '12 at 11:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8962/discussion-between-pp-and-caner) – User7723337 Mar 16 '12 at 11:31
  • I'm not sure if it will work if connection type changes. Why don't you check if there is internet connection like I describe in `Edit3`? – Caner Mar 16 '12 at 11:34
  • I don't want to check if we are connected ti wifi or not, but i want to check if we have connection to internet. For that i am using HttpGet to send and request to server and depending on the response i am deciding whether i am connected to internet or not. – User7723337 Mar 16 '12 at 11:42