最近我想学习Android编程。在学习此教程时,发现在Android 9 (API level 28)上无法从Android模拟器连接到本地的nodejs服务器。如果将所有构建依赖项更改为使用较低的API级别(<=27),则可以成功连接。根据我所了解的Android 9的行为变化,我不知道可能会导致这种情况。以下是我认为关键的代码:
public class ChatBoxActivity extends AppCompatActivity {
//declare socket object
private Socket socket;
public String Nickname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_box);
// get the nickame of the user
Nickname = (String) getIntent().getExtras().getString(MainActivity.NICKNAME);
//connect you socket client to the server
try {
socket = IO.socket("http://192.168.2.106:3000");
socket.connect();
socket.emit("join", Nickname);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}