我正在尝试调用httpClient,响应是“Permission denied(缺少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>
7 回答
您是否尝试在 application 标记上方提供 permission ?
您应该注意 order 中定义了哪些标签 Manifest.xml.
见Manifest的structure .
Edited:
Details:
Manifest 中定义选项卡的顺序:
权限
应用程序
接收器,服务,元数据
权限必须在应用程序标记之外 . 所以你需要在外面移动你的权限
阅读 Structure of the Manifest File . 语法是
Permissions
不应该在Application
Tag
..你必须把它作为Manifest
的孩子 .将用户权限标记放在应用程序标记上方 . 链接:http://developer.android.com/guide/topics/manifest/uses-feature-element.html
对
错误
尝试此操作您授予权限正确但权限必须在应用程序标记之外维护标记的顺序
权限必须是清单 Headers 的直接子项 .