560

I am trying to access HTTP link using HttpURLConnection in Android to download a file, but I am getting this warning in LogCat:

WARN/System.err(223): java.net.SocketException: Permission denied (maybe missing INTERNET permission)

I have added android.Manifest.permission to my application but it's still giving the same exception.

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
rob
  • 5,771
  • 4
  • 23
  • 26
  • Paste your manifest-permission contents, did you grant permissions for INTERNET? – Anthony Forloney Jan 30 '10 at 20:22
  • 1
    i added import android.Manifest.permission; to app,nothing else. What else do i need to add to grant permissions – rob Jan 30 '10 at 20:27
  • 2
    Look at my answer below and see if that helps, and I have no idea what `import android.Manifest.permission` will do for you if you don't have INTERNET permission set in the AndroidManifest.xml file – Anthony Forloney Jan 30 '10 at 20:27

13 Answers13

1221

Assuming you do not have permissions set from your LogCat error description, here is my contents for my AndroidManifest.xml file that has access to the internet:

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET" />
 <application ...
</manifest>

Other than that, you should be fine to download a file from the internet.

Alexis Gamarra
  • 4,362
  • 1
  • 33
  • 23
Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
  • 4
    If you find answers that solve your question or seem helpful, its useful to select as "answered" and/or upvote, it helps out the community for anyone encountering the same problems. Thats not just directed to this question, but other questions you have asked also – Anthony Forloney Jan 30 '10 at 20:31
  • i didn't knew about it, i will do that for sure if this will solve my problem – rob Jan 30 '10 at 20:32
  • report back if you encounter anything different and I will be more than happy to assist. – Anthony Forloney Jan 30 '10 at 20:33
  • the exception I am getting is similar but not quite as the one in the posted question (SocketException with code 1124). If you can please try to help me, I appreciate it. Here's the link to the question I have just posted: http://stackoverflow.com/questions/3791930/android-application-socketexception-permission-denied-no-such-file-or-directory Thanks! – Fabio Milheiro Sep 25 '10 at 00:40
  • 1
    In my case I had to make sure it was the VERY FIRST line after the manifest statement.. anywhere below that and it would not work. v2.2, api level 8, htc g1 – slf Aug 20 '11 at 22:01
  • Is the permission name case sensitive? – Jaime Hablutzel Aug 16 '15 at 21:11
  • Is there a difference between inserting `` inside `manifest` VS `application`? – dialex Mar 14 '16 at 15:37
  • 1
    @dialex AFAIK, there is no child element of `uses-permission` inside the `application` tag, only the `manifest`. – Anthony Forloney Mar 14 '16 at 18:47
102

Permission name is CASE-SENSITIVE

In case somebody will struggle with same issue, it is case sensitive statement, so wrong case means your application won't get the permission.

WRONG

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

CORRECT

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

This issue may happen ie. on autocomplete in IDE

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
  • 1
    Thank you! I wasted my whole day wondering why my app was not working. I am new to android studio and this issue did not occur in Eclipse before :) – InspiredCoder Jun 01 '15 at 06:53
  • 3
    And I wasted my whole day figuring out what's actually difference between them, OMG its *Capital* and *Small* `ANDROID.PERMISSION` and `android.permission` ;) :p – fWd82 Jun 22 '17 at 02:42
  • 2
    Documentation: https://developer.android.com/training/basics/network-ops/connecting.html – quant Mar 08 '19 at 05:53
61
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.photoeffect"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.example.towntour.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <activity
        android:name="com.photoeffect.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>
Teraiya Mayur
  • 1,094
  • 10
  • 18
34

If you are using the Eclipse ADT plugin for your development, open AndroidManifest.xml in the Android Manifest Editor (should be the default action for opening AndroidManifest.xml from the project files list).

Afterwards, select the Permissions tab along the bottom of the editor (Manifest - Application - Permissions - Instrumentation - AndroidManifest.xml), then click Add... a Uses Permission and select the desired permission from the dropdown on the right, or just copy-paste in the necessary one (such as the android.permission.INTERNET permission you required).

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Rogus
  • 1,218
  • 10
  • 11
25

Copy the following line to your application manifest file and paste before the <application> tag.

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

Placing the permission below the <application/> tag will work, but will give you warning. So take care to place it before the <application/> tag declaration.

Ghost-Man
  • 2,179
  • 1
  • 21
  • 25
nitesh
  • 411
  • 7
  • 15
20

FOR FLUTTER DEVELOPERS.

Go to

android/app/main/AndroidManifest.xml

Outside the

application tag

but inside the

manifest tag

Add

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

dev.bojack
  • 753
  • 5
  • 9
14

Add the below line in your application tag:

 android:usesCleartextTraffic="true"

To be look like below code :

<application
    ....
    android:usesCleartextTraffic="true"
    ....>

And add the following tags above of application

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

to be like that :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.themarona.app">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        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>

    </application>
</manifest>
Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53
12

When using eclipse, Follow these steps

  1. Double click on the manifest to show it on the editor
  2. Click on the permissions tab below the manifest editor
  3. Click on Add button
  4. on the dialog that appears Click uses permission. (Ussually the last item on the list)
  5. Notice the view that appears on the rigth side Select "android.permission.INTERNET"
  6. Then a series of Ok and finally save.

Hope this helps

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Mathayo
  • 39
  • 1
  • 3
8

I am late but i want to complete the answer.

An permission is added in manifest.xml like

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

This is enough for standard permissions where no permission is prompted to the user. However, it is not enough to add permission only to manifest if it is a dangerous permission. See android doc. Like Camera, Storage permissions.

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

You will need to ask permission from user. I use RxPermission library that is widely used library for asking permission. Because it is long code which we have to write to ask permission.

RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance // Must be done during an initialization phase like onCreate
rxPermissions
    .request(Manifest.permission.CAMERA)
    .subscribe(granted -> {
        if (granted) { // Always true pre-M
           // I can control the camera now
        } else {
           // Oups permission denied
        }
    });

Add this library to your app

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.tbruyelle:rxpermissions:0.10.1'
    implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
}
Nusatad
  • 3,231
  • 3
  • 11
  • 17
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
4

That may be also interesting in context of adding INTERNET permission to your application:

Google has also given each app Internet access, effectively removing the Internet access permission. Oh, sure, Android developers still have to declare they want Internet access when putting together the app. But users can no longer see the Internet access permission when installing an app and current apps that don’t have Internet access can now gain Internet access with an automatic update without prompting you.

Source: http://www.howtogeek.com/190863/androids-app-permissions-were-just-simplified-now-theyre-much-less-secure/

Bottom line is that you still have to add INTERNET permission in manifest file but application will be updated on user's devices without asking them for new permission.

1

You have to use both Network and Access Network State in manifest file while you are trying load or access to the internet through android emulator.

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

If you are giving only .INTERNET permission, it won't access to the internet.

zemunkh
  • 662
  • 7
  • 13
1

** For Activity Recognition like Foot Step Counter

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

** For Internet

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

** For Call Phone

<uses-permission android:name="android.permission.CALL_PHONE" />
[![enter image description here][1]][1]
smebes
  • 241
  • 3
  • 2
0

If you're using Android Studio, hover over the code that requires the permission and click "Add Permission .."

enter image description here

Then you can check the changes in AndroidManifest.xml with git.

M Imam Pratama
  • 998
  • 11
  • 26