我在尝试使用HttpURLConnection对象连接到localhost时遇到错误“套接字失败:EACCES(Permission denied)”。我在stackoverflow上读取相关问题,例如Android HTTP POST request error - socket failed EACCES (Permission denied)。但我已将所有权限添加到清单
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
我的代码的相关部分
@Override
protected String doInBackground(String... params) {
try {
URL url=new URL("http://192.168.23.1/sozluk/cek.php");
HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
urlConn.setRequestMethod("GET");
urlConn.connect();
InputStream iStream=urlConn.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(iStream));
StringBuilder strBuffer=new StringBuilder();
String strread=null;
while((strread=reader.readLine())!=null){
strBuffer.append(strread);
}
reader.close();
String result=strBuffer.toString();
Log.v("result",result);
return result;
} catch ( Exception e) {
e.printStackTrace();
}
return null;
}
Logcat输出
java.net.SocketException: socket failed: EACCES (Permission denied)
06-29 01:39:31.956 11459-11474/com.foo.foo W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:583)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at java.net.Socket.checkOpenAndCreate(Socket.java:663)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at java.net.Socket.connect(Socket.java:807)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at com.foo.foo.MasterActivity$WordLoader.doInBackground(MasterActivity.java:59)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at com.foo.foo.MasterActivity$WordLoader.doInBackground(MasterActivity.java:50)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-29 01:39:31.961 11459-11474/com.foo.foo W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ at libcore.io.Posix.socket(Native Method)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:568)
06-29 01:39:31.966 11459-11474/com.foo.foo W/System.err﹕ ... 22 more