Search code examples
androidandroid-wifiandroid-networking

Code behaves differently on two different network type


I have following code for testing the server connectivity.
I have a device that has Ethernet as well as Wi-fi connectivity.

When user switch network from Ethernet to wifi or vise verse, i do the test for server connectivity and i check if my server is reachable or not with the new network.

I have following code:

public class TestActivity extends Activity
{
    Button test_btn = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        test_btn = (Button)findViewById(R.id.testButton);
        test_btn.setOnClickListener( new OnClickListener() 
        {
            public void onClick(View v) 
            {
                Log.d("TestApp", "onClick Starting Test");
                startTest();
            }
        });
    } 

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

    class ServerTestThread extends Thread 
    {
        boolean result = false;
        public void run() 
        {   
             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("TestApp", "Ping Result:"+result);
        }
    }

}

This code works fine on my device when i connect my device to internet using Ethernet connectivity, but when i switch from Ethernet to WI-FI this code gives me false result every time.

Using wi-fi i am able to ping to MyServer using the android browser, but from my application i am not able to ping to my server.

Do i need to add something extra to my code to make it work for wifi and Ethernet?

I have also Tried to with InetAddress.getByName("www.MyServer.com").isReachable(timeout) but it also giving me the same results.

Is there any reliable way of implementing ping in Android which will work across differnt platforms.


Solution

  • sorry use this one

    class ServerTestThread extends Thread 
    {
    boolean result = false;
    public void run() 
    {   
         boolean result = false;
         HttpGet request = new HttpGet("www.MyServer.com");
         HttpParams httpParameters = new BasicHttpParams();
    
    
         try
         {
             HttpConnectionParams.setConnectionTimeout(httpParameters, 6000);
             HttpConnectionParams.setSoTimeout(httpParameters, 6000); 
             HttpClient httpClient = new DefaultHttpClient(httpParameters);///write this line below  
             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("TestApp", "Ping Result:"+result);
    }
    

    }