我正在开发一个 android 应用程序,我特别需要仅通过 WiFi 建立 HTTP 连接。在 Android L 及以上版本中似乎有很多与连接相关的变化。
这是我正在使用的一段代码:
ConnectivityManager manager = (ConnectivityManager)
ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] allNetworks = manager.getAllNetworks();
for(Network network : allNetworks) {
NetworkInfo info = manager.getNetworkInfo(network);
if(info.getType() == ConnectivityManager.TYPE_WIFI && info.getState() == NetworkInfo.State.CONNECTED) {
System.out.println("FOUND WIFI NETWORK!");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
manager.bindProcessToNetwork(network);
}
app.network = network;
break;
}
}
根据 ConnectivityManager 和 Network API 文档,我应该能够执行 networkObject.openConnection 以获取绑定该网络的 HttpUrlConnection 。但是我遇到了这种类型的异常
W/System.err: java.net.SocketException: Binding socket to network 586 failed: EPERM (Operation not permitted)
W/System.err: at android.net.Network.bindSocket(Network.java:362)
W/System.err: at android.net.Network.bindSocket(Network.java:331)
W/System.err: at android.net.Network$NetworkBoundSocketFactory.createSocket(Network.java:182)
W/System.err: at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:155)
W/System.err: at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:67)
W/System.err: at com.android.okhttp.Connection.connect(Connection.java:152)
W/System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
W/System.err: at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
W/System.err: at com.mypkg.myapp.utils.HttpRequestBackground.doInBackground(HttpRequestBackground.java:90)
W/System.err: at com.mypkg.myapp.utils.HttpRequestBackground.doInBackground(HttpRequestBackground.java:38)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: Caused by: android.system.ErrnoException: Binding socket to network 586 failed: EPERM (Operation not permitted)
这些是清单中声明的网络相关权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我不太明白这里出了什么问题。
这样做的原因:当启用数据并且它连接的 AP 没有互联网时,Android 不允许我在我的应用程序中使用 wifi 连接。