权限被拒绝(缺少INTERNET权限?):但是获得了许可

时间:2014-08-05 09:38:53

标签: android android-permissions

我试图调用httpClient并且响应是"权限被拒绝(缺少INTERNET权限?)"。在Android的普通浏览器中,我可以毫无问题地打开URL。

 public static String getHttpResponse(URI uri) {
    StringBuilder response = new StringBuilder();
    try {

        HttpGet get = new HttpGet();
        get.setURI(uri);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(get);

        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            Log.d("demo", "HTTP Get succeeded");

            HttpEntity messageEntity = httpResponse.getEntity();
            InputStream is = messageEntity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = br.readLine()) != null) {
                response.append(line);
            }
        }
    } catch (Exception e) {
        Log.e("demo", e.getMessage());
    }
    Log.d("demo", "Done with HTTP getting");
    return response.toString();
}

catch日志告诉我错误:

java.lang.SecurityException: Permission denied (missing INTERNET permission?)
libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
Permission denied (missing INTERNET permission?)

在我的Manifest中是权限集:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="..." >

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-feature android:name="android.hardware.camera" android:required="true" />

    <activity
        android:name=".main"
        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>

7 个答案:

答案 0 :(得分:52)

您是否尝试在应用标记上方提供权限

您应该注意在 Manifest.xml中定义标记的订单

见Manifest的structure

<强>编辑:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    ...>
</application>

<强>详细信息:

清单中定义标签的顺序:

  1. 权限
  2. 应用
  3. 接收方,服务,元数据

答案 1 :(得分:10)

权限必须在应用程序标记之外。所以你需要移出你的权限

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    ... >
</application>

了解 Structure of the Manifest File 。语法是

<manifest>

    <uses-permission />

    <application>
        ....
    </application>

</manifest>

答案 2 :(得分:3)

Permissions不应该在Application Tag中..您必须将其作为Manifest的孩子。

答案 3 :(得分:2)

将您的用户权限标记放在应用程序标记上方。链接:http://developer.android.com/guide/topics/manifest/uses-feature-element.html

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="..." >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-feature android:name="android.hardware.camera" android:required="true" />

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


    <activity
        android:name=".main"
        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>

答案 4 :(得分:1)

<uses-permission/>
    <application/>

<application>

<uses-permission/>
<application/>

答案 5 :(得分:0)

试试这个    您授予权限正确但权限必须在     应用程序标记维护标记的顺序

  <manifest>
      <uses-permission />
      <application>
       ....
      </application>
      <receiver> </receiver>
      <service> </service>
</manifest>

your code is like this way

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="..." >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-feature android:name="android.hardware.camera" android:required="true"/>

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

    <activity
        android:name=".main"
        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>

答案 6 :(得分:0)

权限必须是清单标题的直接子项。