java.lang.SecurityException:Permission denied(缺少INTERNET权限?),即使在android manifest中添加了此权限也是如此

时间:2015-09-28 12:58:17

标签: java android multithreading android-asynctask

我知道其他许多人之前都会问过类似的问题。但是我对此很感兴趣,但我的问题并没有解决堆栈流或其他网站上的先前答案: 这是我的清单文件:

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

我在班上有这个代码:

JSONObject json = jParser.makeHttpRequest("http://sirsaleh.com/atu/atu.php", "GET", params);

但我每次都看到这个运行时错误:

09-28 16:22:28.579    9558-9597/ir.atue.atufeed E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
        at java.net.InetAddress.getAllByName(InetAddress.java:214)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:612)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:495)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:473)
        at ir.atue.atufeed.JSONParser.makeHttpRequest(JSONParser.java:65)
        at ir.atue.atufeed.takeit$LoadAllProducts.doInBackground(takeit.java:106)
        at ir.atue.atufeed.takeit$LoadAllProducts.doInBackground(takeit.java:84)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
 Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
        at libcore.io.Posix.getaddrinfo(Native Method)
        at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
            at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
            at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
            at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:612)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:495)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:473)
            at ir.atue.atufeed.JSONParser.makeHttpRequest(JSONParser.java:65)
            at ir.atue.atufeed.takeit$LoadAllProducts.doInBackground(takeit.java:106)
            at ir.atue.atufeed.takeit$LoadAllProducts.doInBackground(takeit.java:84)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
 Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)

2 个答案:

答案 0 :(得分:3)

以这种方式将您的许可放在清单中:

<manifest ... >

 ...

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

 <application ...

</manifest>

答案 1 :(得分:1)

您运行应用程序的设备(模拟器,Android手机等)可能无法访问您在代码中定义的网络地址。

  

引起:libcore.io.GaiException:getaddrinfo失败:EAI_NODATA(否)   与主机名相关联的地址)

当您收到上述错误时,请尝试使用您设备的原生浏览器打开http地址,并确保您的设备可以访问该地址。否则,您可能应该检查设备的互联网连接和代理设置。