我收到错误
java.net.SocketException:套接字失败:EACCES(权限被拒绝)
这是我将数据发布到服务器的方法
public void postData(String stuff){
//Create a new HTTPClient and Post Header
URL url;
HttpURLConnection con = null;
status.setText("In post");
try{
url = new URL("http://myaddress/gpsin");
con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
con.connect();
//Send Request
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes((stuff));
wr.flush();
wr.close();
InputStream is;
int response = con.getResponseCode();
if(response >= 200 && response<= 399){
//return success
status.setText("Success!");
}else{
//return fail
status.setText("Fail");
}
} catch (Exception e){
e.printStackTrace();
status.setText("In catch");
}finally{
if(con!=null)con.disconnect();
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jake.locationapp">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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
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>
LogCat:
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at libcore.io.IoBridge.socket(IoBridge.java:623)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at java.net.Socket.checkOpenAndCreate(Socket.java:689)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at java.net.Socket.setSoTimeout(Socket.java:543)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.android.okhttp.Connection.connect(Connection.java:158)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.android.okhttp.Connection.connect(Connection.java:174)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:316)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:249)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:397)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.jake.locationapp.MainActivity.postData(MainActivity.java:84)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.jake.locationapp.MainActivity$MyLocationListener.onLocationChanged(MainActivity.java:197)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:285)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:214)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:230)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at android.os.Looper.loop(Looper.java:135)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5430)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at libcore.io.Posix.socket(Native Method)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: at libcore.io.IoBridge.socket(IoBridge.java:608)
09-09 12:43:33.308 11819-11819/com.jake.locationapp W/System.err: ... 21 more
我已经做了相当多的谷歌搜索,但我能找到的只是人们错放了清单中的权限行
答案 0 :(得分:0)
尝试继续而不调用.connect()。文档在这方面有点令人困惑,因为它告诉您返回的URLConnection在创建时没有连接,但是HttpURLConnection示例从不显示对.connect()的实际调用。可能是您已经连接,并且您对.connect()的后续调用尝试在已经完成连接时建立连接,从而在后台导致奇怪的权限问题。 - Larry Schiefer
这很有效!