来自带有 API 30 (android 11) 的三星物理模拟器的错误 Android 11 -> java.net.SocketTimeoutException
Posted
技术标签:
【中文标题】来自带有 API 30 (android 11) 的三星物理模拟器的错误 Android 11 -> java.net.SocketTimeoutException【英文标题】:Error Android 11 -> java.net.SocketTimeoutException from Samsung physical emulator with API 30 (android 11) 【发布时间】:2021-09-11 03:57:42 【问题描述】:我有两个物理模拟器
-
采用 API 30 (android 11) 的三星手机
采用 API 25 (android 7.1) 的三星平板电脑
每当我尝试使用带有 API 25 (android 7.1) 的三星平板电脑运行连接到套接字的应用程序时,我都会完美连接套接字,但如果我使用带有 API 30 (android 11) 的三星手机运行它,我总是在堆栈跟踪中得到以下错误:
java.net.SocketTimeoutException: failed to connect to /192.168.43.1 (port 5050) from /10.144.1.42 (port 50696) after 5000ms
这是我的代码:
public class MainActivity extends AppCompatActivity
AtomicReference<String> serverIP = new AtomicReference<>("");
String localIP;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setWiFiAddress(getApplication());
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.execute(() ->
Socket socket = new Socket();
try
socket.connect(new InetSocketAddress(serverIP.get(), 5050), 5000);
catch (Exception e)
e.printStackTrace();
System.out.println("ok");
);
public Boolean setWiFiAddress(Application application)
ConnectivityManager connectivityManager = (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
Network nw = connectivityManager.getActiveNetwork();
if (nw == null) return false;
NetworkCapabilities actNw = connectivityManager.getNetworkCapabilities(nw);
if (actNw != null && actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))
final WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
InetAddress localIP = getIntAddress(manager.getDhcpInfo().ipAddress);
if(localIP != null)
this.localIP = localIP.getHostAddress();
InetAddress address = getIntAddress(manager.getDhcpInfo().serverAddress);
if (address != null)
serverIP.set(address.getHostAddress());
return true;
serverIP.set("");
return false;
private InetAddress getIntAddress(int hostAddress)
// Get IP Address
byte[] addressBytes = (byte) (0xff & hostAddress),
(byte) (0xff & (hostAddress >> 8)),
(byte) (0xff & (hostAddress >> 16)),
(byte) (0xff & (hostAddress >> 24));
try
return InetAddress.getByAddress(addressBytes);
catch (UnknownHostException e)
return null;
我在网上搜索了很多建议是在<application ....> </application>
AndroidManifest.xml 中添加android:usesCleartextTraffic="true"
,但这并没有解决问题。
我还确保我拥有我的应用所需的所有权限列表:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.hardware.location.gps" />
<uses-permission android:name="android.hardware.location.network" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我想知道为什么我得到这部分错误:from /10.144.1.42 (port 50696)
,因为网络子网是:192.168.43.x,服务器套接字位于 192.168.43.1(它也是 wifi 服务器)。
【问题讨论】:
【参考方案1】:这似乎是安全问题。 如果网络是私有套接字将打开。
【讨论】:
以上是关于来自带有 API 30 (android 11) 的三星物理模拟器的错误 Android 11 -> java.net.SocketTimeoutException的主要内容,如果未能解决你的问题,请参考以下文章
在 Kivy 的 API 30 (Android 11) 中读取和写入文件
在 Android 11 (API 30) 中以编程方式更改状态栏文本颜色
如何在没有系统确认对话框的情况下删除 Android 11 (API 30) 上的文件?