Android determines whether the server is open. I have tried many methods but nothing works (if the server is not open, it will be stuck at HttpResponse). Some people say that higher versions of Android programs do not allow access to the network in the main thread (network streams can be read and written in the main thread). for verification. (Correction: It’s not that access is not allowed, but that it is very time-consuming and will cause death (if it is not time-consuming, it will not be suspended); Correct answer: When the user clicks a button, if a time-consuming operation is performed, it will not be handled well. It will cause the system to freeze and the user experience is very poor. Android goes one step further. If any activity does not respond for more than 5 seconds, it will be forcibly closed. Therefore, we need to start another thread to handle long-time operations, while the main thread Not affected by it, after the time-consuming operation is completed, the message is sent to the main thread, and the main thread performs corresponding processing. Then the message passing and asynchronous processing between threads use Handler. )
method one:
Add in the onCreate() method of xxxActivity
These two lines of code were originally added to the Activity I wrote. This method did not solve my problem. Go on...
solution 2:
Call directly where needed:
The following direct call cannot obtain the return value of the String type. It returns an Object object. For the specific acquisition method, please refer to:
Android has two ways to get the return value of AsyncTask. This article
采用Http方式:
复制代码
public boolean isConnByHttp(){
boolean isConn = false;
URL url;
HttpURLConnection conn = null;
try {
url = new URL("ttp://wl.daishu001.com/YHDriver.asmx");
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(1000*5);
if(conn.getResponseCode()==200){
isConn = true;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
conn.disconnect();
}
return isConn;
}
Android判断服务器是否开启,试了很多方法都不行(若server未开启会卡在HttpResponse那),有人说高版本的Android程序不允许在主线程中访问网络(主线程中可以读写网络流)有待于验证。(更正:不是说的不允许访问,而是很耗时,会假死(不耗时则不会假死);正解:当用户点击一个按钮时如果执行的是一个常耗时操作的话,处理不好会导致系统假死,用户体验很差,而Android则更进一步,如果任意一个Acitivity没有响应5秒钟以上就会被强制关闭,因此我们需要另外起动一个线程来处理长耗时操作,而主线程则不受其影响,在耗时操作完结发送消息给主线程,主线程再做相应处理。那么线程之间的消息传递和异步处理用的就是Handler。)
方法一:
在xxxActivity的onCreate()方法中添加
这两句代码在我写的Activity中本来就添加着,这个方法没有解决我的问题,go on.....
solution 2:
Call directly where needed:
The following direct call cannot obtain the return value of the String type. It returns an Object object. For the specific acquisition method, please refer to:
Android has two ways to get the return value of AsyncTask. This article
采用Http方式:
复制代码
public boolean isConnByHttp(){
boolean isConn = false;
URL url;
HttpURLConnection conn = null;
try {
url = new URL("ttp://wl.daishu001.com/YHDriver.asmx");
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(1000*5);
if(conn.getResponseCode()==200){
isConn = true;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
conn.disconnect();
}
return isConn;
}
采用Http方式:
复制代码
public boolean isConnByHttp(){
boolean isConn = false;
URL url;
HttpURLConnection conn = null;
try {
url = new URL("ttp://wl.daishu001.com/YHDriver.asmx");
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(1000*5);
if(conn.getResponseCode()==200){
isConn = true;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
conn.disconnect();
}
return isConn;
}