简体   繁体   English

使用主机名时 HttpURLConnection 无法解析主机

[英]HttpURLConnection unable to resolve host when using host name

I've checked most of the topics about the same problem, but I couldn't find a solution.我已经检查了关于同一问题的大部分主题,但找不到解决方案。

My Android app connects to a WCF service using the HttpURLConnection and it works fine when I use the IP address of the machine that hosts the service.我的Android应用程序使用HttpURLConnection连接到WCF服务,当我使用托管该服务的机器的 IP 地址时,它工作正常。 However, when I replace it with a host name it can not connect.但是,当我用主机名替换它时,它无法连接。 The connection method that I have is quite standard:我的连接方法很标准:

public static String getData(RequestPackage requestPackage) {
        BufferedReader reader = null;
        HttpURLConnection con = null;
        String uri = requestPackage.getUri();
        String response = "";

        try {
            URL url = new URL(uri);
            con = (HttpURLConnection) url.openConnection();
            con.setReadTimeout(requestPackage.getReadTimeout());
            con.setConnectTimeout(requestPackage.getConTimeout());
            con.setRequestMethod(requestPackage.getMethod());
            con.setDoInput(requestPackage.isDoInput());
            con.setDoOutput(requestPackage.isDoOutput());

            JSONObject user = new JSONObject(requestPackage.getParams());

            OutputStream os = con.getOutputStream(); // the exception is thrown here
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(user.toString());
            writer.flush();
            writer.close();
            os.close();

            // Get response from the service
            int responseCode = con.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                // more code...
            }else
                // more code

The exception is:例外是:

java.net.UnknownHostException: Unable to resolve host "myHostname": No address associated with hostname" java.net.UnknownHostException:无法解析主机“myHostname”:没有与主机名关联的地址“

myHostname is my actual host name. myHostname 是我的实际主机名。

It works fine when I use the same url on my PC (both with the IP and with the host name).当我在我的 PC 上使用相同的 url 时它工作正常(同时使用 IP 和主机名)。

On the phone it works only if i am using the IP. Both in the app or in the mobile browser.在手机上,它仅在我使用 IP 时有效。无论是在应用程序中还是在移动浏览器中。

I have added the necessary permissions in the AndroidManifest.xml :我在AndroidManifest.xml中添加了必要的权限:

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

Could it be something with the permissions to the server that hosts the service?可能是对托管服务的服务器有权限的东西吗? It's an internal server and all machines are connected to an internal Wi-Fi.network.它是一个内部服务器,所有机器都连接到内部 Wi-Fi.network。 But it works with the IP...但它适用于 IP...

EDIT: I forgot to mention that I am using a physical device instead of the emulator.编辑:我忘了提到我使用的是物理设备而不是模拟器。

It is all right.没关系,还不错。 Android tells you that the phone cannot resolve the name "myHostname". Android 告诉您电话无法解析名称“myHostname”。 It depends on the topology of your.network.这取决于您的网络拓扑结构。 I think that the phone is connected with wifi on an access point that has a wired connection to the lan on which there are you pc and your myHostname host.我认为手机在接入点上通过 wifi 连接,该接入点有线连接到您的 PC 和您的 myHostname 主机所在的局域网。 Your pc got a dns server on its lan connection or just resolve the name with lan broadcasting.您的电脑在其局域网连接上有一个 dns 服务器,或者只是通过局域网广播解析名称。 Surely you have not configured a dns server on the connection in use on your phone.您肯定没有在手机上使用的连接上配置 dns 服务器。 If you have a dns server on your lan, than set it on the connection, otherwise I don't think that a phone can resolve by broadcast.如果你的局域网上有一个 dns 服务器,那么在连接上设置它,否则我不认为电话可以通过广播来解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代理主机名,端口,用户名,带有httpurlconnection的密码 - proxy host name,port,username,password with httpurlconnection 使用模拟器和HttpURLConnection的未知主机异常 - Unknown Host Exception using emulator and HttpURLConnection java.net.UnknownHostException:无法解析主机“@MYIPADDRESS,当使用android + mongodb时 - java.net.UnknownHostException: Unable to resolve host "@MYIPADDRESS, when using android + mongodb Hyperledger Fabric - 无法解析主机 - 当向链码发布资产时 - Hyperledger Fabric - Unable to resolve host - when issue an asset to chaincode 在S3中使用加速模式时无法解决主机错误 - Unable to resolve host error while using accelerate mode in s3 无法解析主机名意味着如何在 android 工作室的异步任务中解决它 - Unable to resolve host name means how to resolve it in async task in android studio JSOUP和Android模拟器-无法解析主机 - JSOUP and Android Emulator - unable to resolve host Android AWS Cognito:无法解析主机 - Android AWS Cognito: unable to resolve host IOException无法解析主机,没有与主机名关联的地址 - IOException Unable to resolve host,No address associated with hostname 无法解析主机:没有与主机名关联的地址 - Unable to resolve host: No address associated with hostname
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM