Java Android 上的 InetAddress.getByName

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2096874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:30:56  来源:igfitidea点击:

InetAddress.getByName on Android

javaandroidsockets

提问by Stefan Steiger

I do a:

我做一个:

java.net.InetAddress serverAddr;
try {
    serverAddr = java.net.InetAddress.getByName(Server.SERVERNAME);
}
catch (java.net.UnknownHostException exception) {
    //System.err.println ("wrong server name !!!");
    HelloWorldActivity.tv.setText("wrong server name !!!");
    return;
}

in my android application, but it's never resoling the hostname, it always throws an exception, no matter what name I use.

在我的 android 应用程序中,但它从不重新解析主机名,无论我使用什么名称,它总是抛出异常。


But using the internet on the same emulator works, and I've added


但是在同一个模拟器上使用互联网是有效的,我已经添加了

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

to AndoidManifest.xml

到 AndoidManifest.xml

and here's the server class for those who assume I have none

这是那些认为我没有的人的服务器类

public class Server
{
    public static String SERVERNAME = "monster.idsoftware.com";
    public static String SERVERIP = "209.85.129.99";
    public static int SERVERPORT = 27950;
    public static int PROTOCOL = 68;
}

采纳答案by Stefan Steiger

I've found the answer. For whatever reason, you have to use:

我找到了答案。无论出于何种原因,您都必须使用:

java.net.InetAddress[] x= java.net.InetAddress.getAllByName(Server.SERVERNAME) ; HelloWorldActivity.tv.setText("Address: "+x[0].getHostAddress());

java.net.InetAddress[] x= java.net.InetAddress.getAllByName(Server.SERVERNAME) ; HelloWorldActivity.tv.setText("Address: "+x[0].getHostAddress());

回答by adn37

It is strange that you have to do so. java.net.InetAddress.getByNameworks for me, out of the box.

奇怪的是你必须这样做。java.net.InetAddress.getByName对我有用,开箱即用。

There are some (on-going) issues related to DNS resolution in the Android emulator, so that might be it.

Android 模拟器中存在一些与 DNS 解析相关的(正在进行的)问题,所以可能就是这样。

回答by alex2k8

Don't know if it was a typo, but you said you have:

不知道这是否是一个错字,但你说你有:

<use-permission id="android.permission.INTERNET" />

But it have to be:

但它必须是:

<uses-permission android:name="android.permission.INTERNET" />

I tried getByName and it works fine.

我试过 getByName 并且它工作正常。

May be you fixed your permissions and switched from getByName to getAllByName at the same time? Just curious, if you can confirm that getByName still does not work for you?

您是否可以修复您的权限并同时从 getByName 切换到 getAllByName?只是好奇,如果您能确认 getByName 仍然对您不起作用?

回答by Reg Edit

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:

我遇到了类似的问题,我发现在某些版本的 android(来自蜂窝)中,默认情况下不允许从主线程执行网络操作。所以你可以通过两种方式解决它。在不同的线程中执行操作或允许在主线程中进行网络操作。要做到这一点,请使用以下内容:

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