What permission do I need to access Internet from

2018-12-31 07:43发布

I get the following Exception running my app:

java.net.SocketException: Permission denied (maybe missing INTERNET permission)

How do I solve the missing permission problem?

11条回答
弹指情弦暗扣
2楼-- · 2018-12-31 07:48

Add the INTERNET permission to your manifest file.

You have to add this line:

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

outside the application tag in your AndroidManifest.xml

查看更多
不流泪的眼
3楼-- · 2018-12-31 07:48

Just put below code in AndroidManifest :

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
查看更多
春风洒进眼中
4楼-- · 2018-12-31 07:48

As per current versions, Android doesn't ask for permission to interact with the internet but you can add the below code which will help for users using older versions Just add these in AndroidManifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
查看更多
骚的不知所云
5楼-- · 2018-12-31 07:55

To request for internet permission in your code you must add these to your AndroidManifest.xml file

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

For more detail explanation goto https://developer.android.com/training/basics/network-ops/connecting

查看更多
余生无你
6楼-- · 2018-12-31 07:56
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
查看更多
忆尘夕之涩
7楼-- · 2018-12-31 08:01

forget about adding the permission into the manifest Add this code as a 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;
}

and write this in your Main

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);
    }
查看更多
登录 后发表回答