简体   繁体   English

代码在两种不同的网络类型上表现不同

[英]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. 我有一台具有以太网和Wi-Fi连接的设备。

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. 当用户将网络从以太网切换到wifi或vise verse时,我会进行服务器连接测试,并检查我的服务器是否可以通过新网络访问。

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. 当我使用以太网连接将设备连接到互联网时,此代码在我的设备上正常工作,但是当我从以太网切换到WI-FI时,此代码每次都会给我错误的结果。

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. 使用wi-fi,我可以使用android浏览器ping到MyServer,但是从我的应用程序中我无法ping到我的服务器。

Do i need to add something extra to my code to make it work for wifi and Ethernet? 我是否需要在代码中添加额外的东西才能使其适用于wifi和以太网?

I have also Tried to with InetAddress.getByName("www.MyServer.com").isReachable(timeout) but it also giving me the same results. 我也尝试使用InetAddress.getByName("www.MyServer.com").isReachable(timeout)但它也给了我相同的结果。

Is there any reliable way of implementing ping in Android which will work across differnt platforms. 有没有可靠的方法在Android中实现ping,它可以在不同的平台上运行。

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SimpleDateFormat在不同的平板电脑上的行为不同 - SimpleDateFormat behaves differently on different tablet 代码在Android和Windows中的行为有所不同 - Code behaves differently in Android and Windows Android UI的行为与不同的显示硬件不同 - Android UI behaves differently with different display hardware 为什么同一段(简单的)Java代码在不同的Android设备上的行为会大不相同? - Why same piece of (simple) Java code behaves very differently on different Android devices? 检查网络中两种不同类型的设备 - Check Network for two different type of devices android opengl应用在设备上的行为不同,z-far是否不同? - android opengl app behaves differently on device, z-far is different? 在 Android Studio 的不同活动布局中,Drawable 的行为不同 - Drawable behaves differently in different activity layouts in Android Studio Flutter_TTS package 在不同设备上表现不同 - Flutter_TTS package behaves differently on different devices JellyBean 与图片捕捉的行为不同 - JellyBean behaves differently with picture capturing GridView在手机和仿真器上的行为有所不同 - GridView Behaves Differently on phone and Emulator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM