HttpUrlConnection request error (Missing Internet permission when the permission is enabled)

空扰寡人 提交于 2019-12-08 03:27:58

问题


I'm having the following error on a few of my users:

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

The app requires internet to do it's most basic function, I don't think a user have removed the permission using root or something like that.

Additionally, this error was sent to me with my own error reporting tool. I have made the upload with the registered error and at that moment the permission was available.

There is no restriction on device model of android version that has the error.

Example of a part of the manifest:

<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"/>

[...]

Any clue is appreciated,

Thanks in advance.


回答1:


This will help you. Write the following in your onCreate method

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);
    }

And get this method

 public static boolean hasPermissions(Context context, String... permissions)
{
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null)
    {
        for (String permission : permissions)
        {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED)
            {
                return false;
            }
        }
    }
    return true;
}

The application will ask for only those permission which the application needs, that too only once. If it works upvote and do let me know. It worked for me




回答2:


HttpUrlConnection request also requires android.permission.ACCESS_NETWORK_STATE. Do you also have this permission listed in the manifest?

Please post your manifest file so we can see that android.permission.ACCESS_NETWORK_STATE, and android.permission.INTERNET are both listed without any spelling, capitalization, syntax errors.

The issue can also happen if a user has rooted their Android device and tinkered with the permissions. How do you know the user has not rooted the device? Can you reproduce the problem on any of your own devices?

See more detail on this issue here: SecurityException: Permission denied (missing INTERNET permission?)



来源:https://stackoverflow.com/questions/34163435/httpurlconnection-request-error-missing-internet-permission-when-the-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!