1

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.

Gabriel Falcone
  • 678
  • 5
  • 17

2 Answers2

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

Vaibhav Joshi
  • 145
  • 1
  • 5
0

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?)

Community
  • 1
  • 1
joshgoldeneagle
  • 4,616
  • 2
  • 23
  • 30
  • I can't upload it here but I'm 100% sure that there isn't a problem with the manifest. I check manually the permissions before even allowing that to happen, and the permission was confirmed. The code shouldn't even reach that point if the permissions aren't available. Since the error was uploaded, the permission was available at some point. I have read that link, and I have my error catching mechanisms to avoid crashing, but I really wanted to understand what was happening and how to even handle correctly, and not just doing a try-catch. – Gabriel Falcone Dec 08 '15 at 19:04
  • Because this doesn't happen in just one app. I do have a library that is being used in a few apps, and it happens in more than just one. Although not often, it still have some recurrent entries. I didn't think it would be helpful to upload to upload the manifest of all the apps that usually works just fine but a few requests still fail. But as you asked, I have pasted the beginning of the manifest of one of the apps. Thanks for your attention. – Gabriel Falcone Dec 08 '15 at 20:10