[英]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.