问题
I am trying to connect to a chat server on the internet from an Android app so first I test whether I have contact with the server from an AsyncTask:
@Override
protected Void doInBackground(SocketAndEditText... soEd) {
try{
InetAddress address1 = InetAddress.getByName("130.237.161.23");
boolean reachable = address1.isReachable(4456);
//...
} catch (Exception e) {
//...
}
}
When I run the above code the variable reachable
gets the value false
but when I run the same code as a simple Java console application I get true
:
import java.net.InetAddress;
public class Test {
public static void main(String[] args) {
try{
InetAddress address1 = InetAddress.getByName("130.237.161.23");
boolean reachable = address1.isReachable(4456);
System.out.println(reachable);
} catch (Exception e) {
//...
}
}
}
In my manifest file I have <uses-permission android:name="android.permission.INTERNET" />
Why does it not work in Android but works as a Java application?
来源:https://stackoverflow.com/questions/10575747/android-app-fails-to-get-access-to-the-network