Android <uses-permission> 未请求权限
Posted
技术标签:
【中文标题】Android <uses-permission> 未请求权限【英文标题】:Android <uses-permission> not requesting permission 【发布时间】:2016-12-14 11:08:07 【问题描述】:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.neo.gson">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="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>
HttpURLConnection 使我的应用程序崩溃,因为即使我在清单文件中使用标签也没有授予互联网权限
It says no permissions requested
logcat 提取:
08-08 16:50:32.167 3625-3625/com.example.neo.gson E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.neo.gson, PID: 3625
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.neo.gson/com.example.neo.gson.MainActivity: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:436)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
at com.example.neo.gson.MainActivity.onCreate(MainActivity.java:47)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
你能帮忙吗?
我正在使用 Marshmallow,我的 targetSdkVersion 是 23 我在 onCreate 方法中调用 HttpURLConnection
【问题讨论】:
你用棉花糖吗?你在哪里调用你的 httpurlconnection? "HttpURLConnection 使我的应用程序崩溃" -- 请编辑您的问题并发布 minimal reproducible example,包括崩溃的代码和崩溃的完整 Java 堆栈跟踪。 “它说没有请求权限”——列出了dangerous
权限;你的权限都没有protectionLevel
。
请问您的应用项目的目标编译版本是哪个Android版本?
是的,我使用 Marshmallow,targetSdkVersion 23
我在 onCreate 方法中调用 HttpURLConnection
【参考方案1】:
感谢@JoxTraex 解决了问题
您不能在主线程上执行任何网络调用。使用 AsyncTask 执行您的网络调用。
【讨论】:
【参考方案2】:您不能在主线程上执行任何网络调用。使用 AsyncTask 执行您的网络调用。
【讨论】:
以上是关于Android <uses-permission> 未请求权限的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 网络请求报错 Failed host lookup: