【问题标题】:Why does Android 10 HTTPUrlConnection fail? Worked fine on 9为什么 Android 10 HTTPUrlConnection 失败? 9日工作正常
【发布时间】:2019-10-17 11:55:54
【问题描述】:

使用此代码

  URL url = new URL(data.replaceAll(" ", "%20"));
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setReadTimeout(12000);
        urlConnection.setConnectTimeout(12000);
        int xy = urlConnection.getResponseCode();

我遇到了这个异常:

data = "http://myrpd.us/api/staff1?lang=eng"
urlConnection = {HttpURLConnectionImpl@11410}    "com.android.okhttp.internal.huc.HttpURLConnectionImpl:http://myrpd.us/api/staff1?lang=eng"
e = {UnknownHostException@11411} "java.net.UnknownHostException: Unable to resolve host "myrpd.us": No address associated with hostname"

该 URL 在 9 和浏览器中运行良好。 我已经阅读了 10 的所有新隐私信息。我在清单中有这个:

android:usesCleartextTraffic="true"

【问题讨论】:

标签: android exception httpurlconnection


【解决方案1】:

没有互联网连接时也可能出现此错误

java.net.UnknownHostException: Unable to resolve host 

您可以检查您的 Android 10 设备上的互联网连接吗?

【讨论】:

  • 模拟器/模拟器出现这个问题,我的Mac明显有网络连接。
  • 我已经试过你的代码来确定。它在我的模拟器(Nexus 5X API 29 Android 10)上运行。最后,确保模拟器的 Wi-Fi 或移动数据已打开。我就是这么想的。
  • 这是正确答案。我以前从未见过没有通过 Android Studio 自动运行 wifi 的模拟器启动。我认为这个问题与 10 的隐私功能有关。谢谢serkancay。
猜你喜欢
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 1970-01-01
相关资源
最近更新 更多