Boris
Boris

Reputation: 8951

Internet access with Android M

I'm trying to do HTTP requests in Anrdoid M. But every time I get the error:

java.lang.SecurityException: Permission denied (missing INTERNET permission?)

As far as I know every app has the Internet permission by default in Android M. So why does this error occur?

I also tried adding the permission to my manifest and to get a runtime permission, but both without success.

<?xml version="1.0" encoding="utf-8"?>

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

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

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Upvotes: 1

Views: 427

Answers (4)

Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6823

enter image description here

You must have the Internet permission in order to get access to the internet. Use <uses-permission android:name="android.permission.INTERNET"/> where I showed.

Upvotes: 0

William R
William R

Reputation: 64

You must write the permission outside the application tag

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

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

To

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Upvotes: 2

Jerassi Ferrer
Jerassi Ferrer

Reputation: 284

As for the specific android.permission.INTERNET permission, it is still mandatory for apps that will access the Internet. If a developer were to publish an app without defining it in the Android manifest, an exception will be thrown the first time a connection attempt is made, and the app will possibly crash. This is no different than before.

So you can check in your manifest the permition for internet and network state

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

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007276

As far as I know every APp has INTERNET permission by default in Android M

No, they do not.

So why does this error occur?

Apparently, you do not have the <uses-permission> element, or it is in the wrong spot.

and to get a runtime permission

The INTERNET permission is not dangerous and so does not require with the runtime permission system.

Upvotes: 2

Related Questions