Connection refused exception occurs when I use an android real machine to connect to the PC server to download files.
After checking, the problem is solved because the mobile phone is not connected to the computer's wifi network (network sharing) and the mobile phone can use the computer's wifi network to connect to the network normally.
Note that the mobile phone and computer must be in a LAN and use the lan ip address of the computer, for example, 192.168.1.xxx: 8080 (pc ip address)/xxxx to access the computer.
This error leads to a problem:
Determine whether the network is connected to the function code:
Connectivitymanager = (connectivitymanager) Context
. Getapplicationcontext (). getsystemservice (
Context. connectivity_service );
If (Manager = NULL ){
Return false;
}
Networkinfo = manager. getactivenetworkinfo ();
If (networkinfo = NULL |! Networkinfo. isavailable ()){
Return false;
}
Return true;
The code above checks whether the network is connected. I found a problem.
In fact, this function can only determine whether the network is enabled. For example, if the Wi-Fi network switch on the mobile phone is enabled, this Code considers network connection as a matter of fact. For example, if we connect to CMCC, although connected (WiFi enabled) however, you still cannot access the Internet (you need to enter the user name and password on the webpage, you know ), therefore, this function is not accurate. It can only be used to determine whether the network is opened or not. However, in many cases, if a user opens a connection, the network connection will be guaranteed) there should be few cases where WiFi is turned on and then no network connection (for example, if a user does not access the Internet, but the user is a Tom and does not know that he has enabled the Wi-Fi connection ).
In fact, this accuracy issue is acceptable.
I think the best way to determine whether the network is connected is to ping www.baidu.com to check the return value, but I have not tried it yet.