HttpUrlConnection请求错误(启用权限时缺少Internet权限)

时间:2015-12-08 18:35:45

标签: android android-networking

我的一些用户遇到以下错误:

Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    at java.net.InetAddress.getAllByName(InetAddress.java:214)
    at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
    at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
    at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
    at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
    at [my communication HttpUrlConnection request]
    ... 12 more
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    at libcore.io.Posix.getaddrinfo(Native Method)
    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
    ... 25 more
Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
    ... 28 more

该应用程序需要互联网才能完成最基本的功能,我认为用户不会使用root或类似内容删除权限。

此外,此错误是通过我自己的错误报告工具发送给我的。我已使用已注册的错误进行上传,此时该权限已可用。

对于有错误的android版本的设备型号没有限制。

清单的一部分示例:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

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

[...]

感谢任何线索,

提前致谢。

2 个答案:

答案 0 :(得分:1)

这会对你有所帮助。 在onCreate方法中写下以下内容

 ArrayList<Contact> selectedContacts = data.getParcelableArrayListExtra("SelectedContacts");

并获得此方法

int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.CAMERA};

    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }

应用程序只会询问应用程序需要的权限,也只需要一次。如果它起作用,请告诉我。它对我有用

答案 1 :(得分:0)

HttpUrlConnection请求还需要android.permission.ACCESS_NETWORK_STATE。您是否也在清单中列出了此权限?

请发布您的清单文件,以便我们可以看到android.permission.ACCESS_NETWORK_STATE和android.permission.INTERNET都列出时没有任何拼写,大小写,语法错误。

如果用户已植根其Android设备并修改了权限,则也会出现此问题。你怎么知道用户没有植根设备?你能在自己的设备上重现这个问题吗?

在此处查看有关此问题的更多详细信息:SecurityException: Permission denied (missing INTERNET permission?)