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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
ok, when you want to run for the 2nd time what do you do? do you call startTest() again?
yes i call startTest() again. and that time my test fails and also in every 2nd run it gives me Exception.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.