Code behaves differently on two different types of network

I have the following code for testing server connectivity.
I have a device with Ethernet and Wi-Fi.

When the user switches the network from Ethernet to Wi-Fi or vise verse, I do a test to connect to the server and I check if my server is available or not with the new network.

I have the 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 the internet using an Ethernet connection, but when I switch from Ethernet to WI-FI this code gives me a false result every time.

Using wi-fi I can ping to MyServer using android browser, but from my application I cannot ping my server.

Do I need to add something extra to my code to make it work for Wi-Fi and Ethernet?

I also tried with InetAddress.getByName("www.MyServer.com").isReachable(timeout)

but also gave me the same results.

Is there a reliable way to implement ping in Android that will work across platforms.

+3


source to share


1 answer


Sorry use this

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);
}

      



}

+1


source







All Articles