简体   繁体   English

Android API 23 java.net.UnknownHostException:无法解析主机“URL”:没有与主机名关联的地址

[英]Android API 23 java.net.UnknownHostException: Unable to resolve host “URL”: No address associated with hostname

Im facing a big problem, Im trying to connect to an URL using a GET method. 我面临一个大问题,我试图使用GET方法连接到URL。 It give me an error java.net.UnknownHostException: Unable to resolve host "URL": No address associated with hostname. 它给我一个错误java.net.UnknownHostException:无法解析主机“URL”:没有与主机名关联的地址。

I have been reading some post and all people say it is about permissions.My permissions are: 我一直在阅读一些帖子,所有人都说这是关于权限。我的权限是:

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

I can access to that URL by using chrome in my mobile, it means that wifi is working. 我可以通过在我的手机中使用chrome来访问该URL,这意味着wifi正在运行。

I am using a Motorola g 2, API 23. 我使用的是Motorola g 2,API 23。

  1. Printing the response on the Log ? 在日志上打印响应?

  2. Did you try switching of the WiFi or phone or both and tried again ? 您是否尝试过切换WiFi或手机或两者并重试?

  3. This problem can occur when you are on VPN. 当您在VPN上时,可能会发生此问题。 I had a similar issue. 我有一个类似的问题。 After disconnecting from the VPN I tried connecting again. 断开VPN连接后,我再次尝试连接。 It got connected. 它有联系。

  4. Also try rechecking the URL since the UnknownHostException is popping up there is high possibility that the URL string you are passing in the program(URL class probably) is wrong. 同时尝试重新检查URL,因为弹出UnknownHostException,很可能你在程序中传递的URL字符串(可能是URL类)是错误的。

Hope it helps. 希望能帮助到你。

You have two solutions for your problem. 您有两个解决方案可以解决您的问题 The quick one is to lower targetApi to 22 (build.gradle file). 快速的是将targetApi降低到22(build.gradle文件)。 Second is to use the new runtimePermission model: Since your target api is 23 you should add the permissions on runtime too. 其次是使用新的runtimePermission模型:由于你的目标api是23,你也应该在运行时添加权限。

if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.INTERNET) {

    // Should we show an explanation?
    if (shouldShowRequestPermissionRationale(
            Manifest.permission.INTERNET)) {
        // Explain to the user why we need to read the contacts
    }

    requestPermissions(new String[]{Manifest.permission.INTERNET},
            MY_PERMISSIONS_REQUEST_INTERNET);

    // MY_PERMISSIONS_REQUEST_INTERNET is an
    // app-defined int constant

    return;
}

Sniplet found here: https://developer.android.com/preview/features/runtime-permissions.html Sniplet在这里找到: https//developer.android.com/preview/features/runtime-permissions.html

暂无
暂无

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

相关问题 java.net.UnknownHostException:无法解析主机没有与主机名关联的地址 - java.net.UnknownHostException: Unable to resolve host No address associated with hostname java.net.UnknownHostException:无法解析主机“<url> &quot;: 没有与主机名和字符 0 处的输入结束关联的地址 - java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname and End of input at character 0 of Android改装-HTTP失败:java.net.UnknownHostException:无法解析主机{我的基本url}:没有与主机名关联的地址 - Android Retrofit - HTTP FAILED: java.net.UnknownHostException: Unable to resolve host {my base url}: No address associated with hostname java.net.UnknownHostException:无法解析主机“ s3-ap-northeast-1.amazonaws.com”:没有与主机名关联的地址 - java.net.UnknownHostException: Unable to resolve host “s3-ap-northeast-1.amazonaws.com”: No address associated with hostname :java.net.UnknownHostException:无法解析主机“hosturl”:没有与主机名关联的地址 - : java.net.UnknownHostException: Unable to resolve host “hosturl”: No address associated with hostname 非严重异常:java.net.UnknownHostException无法解析主机“ mysite.com”:没有与主机名关联的地址 - Non-fatal Exception: java.net.UnknownHostException Unable to resolve host “mysite.com”: No address associated with hostname java.net.UnknownHostException无法解析主机“accounts.google.com”:在bigquery中插入行时没有与主机名关联的地址 - java.net.UnknownHostException Unable to resolve host “accounts.google.com”: No address associated with hostname while inserting rows in bigquery Android 6.0:java.net.UnknownHostException:无法解析主机。 在Android 7.0上正常工作 - Android 6.0 : java.net.UnknownHostException: Unable to resolve host. Working fine in Android 7.0 java.net.UnknownHostException:无法解析主机“api.themoviedb.org” - java.net.UnknownHostException: Unable to resolve host "api.themoviedb.org java.net.UnknownHostException:无法解析主机“@MYIPADDRESS,当使用android + mongodb时 - java.net.UnknownHostException: Unable to resolve host "@MYIPADDRESS, when using android + mongodb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM