InetAddress.getByName on Android

前端 未结 4 1775
别跟我提以往
别跟我提以往 2020-12-11 17:53

I do a:

java.net.InetAddress serverAddr;
try {
    serverAddr = java.net.InetAddress.getByName(Server.SERVERNAME);
}
catch (java.net.UnknownHostException exc         


        
4条回答
  •  再見小時候
    2020-12-11 18:05

    I was having the similar issue and I found out that in some versions of android (from honeycombs) it's not allowed by default to perform network operation from main thread. So you can resolve it in 2 ways. Perform operation in different thread or allow to make network operation in main thread. To do that use something like this:

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
    StrictMode.setThreadPolicy(policy);
    

提交回复
热议问题