我的客户端代码使我的 android 应用程序崩溃
My client code makes my android app crash
所以我有这个客户端代码,我在 Eclipse
测试过它在那里工作
try {
// Establish connection
Socket socket = new Socket(InetAddress.getByName("192.168.1.104"), 9000);
// Request data
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
msg = "IDGN:" + "1";
System.out.println(msg);
outputStream.write(msg.getBytes());
socket.shutdownOutput();
scanner = new DataInputStream(inputStream);
restaurante = scanner.readUTF();
System.out.println(restaurante);
// Shut down socket
socket.shutdownInput();
socket.close();
} catch (IOException io) {
io.printStackTrace();
}
但是当我尝试在 Android 应用程序中执行相同操作时它崩溃了
try {
// Establish connection
Socket socket = new Socket(InetAddress.getByName("192.168.1.104"), 9000);
// Request data
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
msg = "IDGN:" + "1";
outputStream.write(msg.getBytes());
socket.shutdownOutput();
scanner = new DataInputStream(inputStream);
restaurante = scanner.readUTF();
View b = findViewById(R.id.texto);
b.setVisibility(View.VISIBLE);
String ss = "Valore el servicio recibido en: " + restaurante;
((TextView) b).setText(ss);
// Shut down socket
socket.shutdownInput();
socket.close();
} catch (IOException io) {
io.printStackTrace();
}
我只是不知道为什么,我试图让它等待数据准备好,将其更改为 OutputStream
并从扫描仪读取一行,直接从中读取字节OutputStream
.
服务器似乎在两种情况下(Android 和 Eclipse)都能正常接收数据并发送正确的答案。
完成后关闭或刷新输出流。我认为它导致应用内存泄漏
您不能在同一线程中同时进行网络 I/O 和 UI 操作,例如 TextView#setText()
。尝试在 UI 线程上执行网络 I/O 会导致 NetworkOnMainThreadException
。尝试在非 UI 线程上执行 UI 操作会导致 CalledFromWrongThreadException
。我猜你的代码在其中之一上崩溃了。
有一些高级线程机制,例如 rxjava 调度程序或 kotlin 协程。在不添加额外的库依赖项的情况下,您可以尝试将网络代码移动到 AsyncTask
.
另请参阅:
How do I fix android.os.NetworkOnMainThreadException?
Unfortunately MyApp has stopped. How can I solve this?
所以我有这个客户端代码,我在 Eclipse
测试过它在那里工作
try {
// Establish connection
Socket socket = new Socket(InetAddress.getByName("192.168.1.104"), 9000);
// Request data
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
msg = "IDGN:" + "1";
System.out.println(msg);
outputStream.write(msg.getBytes());
socket.shutdownOutput();
scanner = new DataInputStream(inputStream);
restaurante = scanner.readUTF();
System.out.println(restaurante);
// Shut down socket
socket.shutdownInput();
socket.close();
} catch (IOException io) {
io.printStackTrace();
}
但是当我尝试在 Android 应用程序中执行相同操作时它崩溃了
try {
// Establish connection
Socket socket = new Socket(InetAddress.getByName("192.168.1.104"), 9000);
// Request data
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
msg = "IDGN:" + "1";
outputStream.write(msg.getBytes());
socket.shutdownOutput();
scanner = new DataInputStream(inputStream);
restaurante = scanner.readUTF();
View b = findViewById(R.id.texto);
b.setVisibility(View.VISIBLE);
String ss = "Valore el servicio recibido en: " + restaurante;
((TextView) b).setText(ss);
// Shut down socket
socket.shutdownInput();
socket.close();
} catch (IOException io) {
io.printStackTrace();
}
我只是不知道为什么,我试图让它等待数据准备好,将其更改为 OutputStream
并从扫描仪读取一行,直接从中读取字节OutputStream
.
服务器似乎在两种情况下(Android 和 Eclipse)都能正常接收数据并发送正确的答案。
完成后关闭或刷新输出流。我认为它导致应用内存泄漏
您不能在同一线程中同时进行网络 I/O 和 UI 操作,例如 TextView#setText()
。尝试在 UI 线程上执行网络 I/O 会导致 NetworkOnMainThreadException
。尝试在非 UI 线程上执行 UI 操作会导致 CalledFromWrongThreadException
。我猜你的代码在其中之一上崩溃了。
有一些高级线程机制,例如 rxjava 调度程序或 kotlin 协程。在不添加额外的库依赖项的情况下,您可以尝试将网络代码移动到 AsyncTask
.
另请参阅:
How do I fix android.os.NetworkOnMainThreadException?
Unfortunately MyApp has stopped. How can I solve this?