Android app fails to get access to the network

血红的双手。 提交于 2019-12-13 04:17:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!