权限被拒绝(缺少 INTERNET 权限?):但已授予权限
Posted
技术标签:
【中文标题】权限被拒绝(缺少 INTERNET 权限?):但已授予权限【英文标题】:Permission denied (missing INTERNET permission?): But permission is given 【发布时间】:2014-09-27 22:12:22 【问题描述】:我正在尝试调用 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?)
在我的清单中是权限集:
<?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>
【问题讨论】:
确保你提到了 标签之外的所有权限。 请将清单示例更改为正确的示例。 所有权限必须在 标签之外和您是否尝试在 application 标记上方授予 permission?
您应该注意 Manifest.xml 中定义标签的顺序。
参见清单的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>
详情:
Manifest中定义选项卡的顺序:
-
权限
应用程序
接收器、服务、元数据
【讨论】:
【参考方案2】:权限必须在应用程序标签之外。所以你需要把你的权限移到外面
<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>
【讨论】:
【参考方案3】:Permissions
不应该在Application
Tag
.. 你必须把它作为Manifest
.. 的孩子。
【讨论】:
【参考方案4】:将您的用户权限标签放在应用程序标签上方。链接: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>
【讨论】:
【参考方案5】:对
<uses-permission/>
<application/>
错了
<application>
<uses-permission/>
<application/>
【讨论】:
【参考方案6】:试试这个 您授予的权限正确,但权限必须在 应用标签维护标签的顺序
<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>
【讨论】:
【参考方案7】:权限必须是清单标题的直接子级。
【讨论】:
以上是关于权限被拒绝(缺少 INTERNET 权限?):但已授予权限的主要内容,如果未能解决你的问题,请参考以下文章