Android 应用在 while() 时崩溃
Android App crashes on while()
在我的 android 应用程序中,我正在创建一个 Socket 和一个 BufferedReader。如果我只读了 BufferedReader 的一行,我就会从服务器得到响应。但是,如果我尝试使用 while((message = br.readLine()) != null)
应用程序崩溃。任何想法可能是什么问题?
代码
public void connecting(String uid) {
uuid = uid;
try {
client = new Socket("127.0.0.1", 8234);
try {
tv = (TextView) chat.findViewById(R.id.textView);
pw = new PrintWriter(client.getOutputStream());
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
new Thread(new Runnable() {
@Override
public void run() {
try {
// If i just try this, it works
tv.append("\n"+br.readLine());
// If I'm trying this, it crashes
String message = "";
while((message = br.readLine()) != null) {
tv.append("\n"+message);
}
} catch (IOException e) {
tv.append(e.getMessage());
}
}
}).start();
tv.append("Connected!");
pw.println(uuid+":B31TR1TT");
pw.flush();
} catch (IOException e) {
tv.append(e.getMessage());
}
} catch (IOException e) {
tv.append(e.getMessage());
}
}
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
尝试使用此代码代替 while 循环代码
或访问此 link http://hastebin.com/inecerakes.java
正如 algui91 所说,将网络调用移至单独的线程。严格模式错误 (violation=4) 表示 ui 线程上的网络调用。
只需将网络调用重构为后台任务(服务、asynctask 或其他),问题就会消失。
将 UI 与业务逻辑或网络通信分开。 test/debug.
总是更好更容易
在我的 android 应用程序中,我正在创建一个 Socket 和一个 BufferedReader。如果我只读了 BufferedReader 的一行,我就会从服务器得到响应。但是,如果我尝试使用 while((message = br.readLine()) != null)
应用程序崩溃。任何想法可能是什么问题?
代码
public void connecting(String uid) {
uuid = uid;
try {
client = new Socket("127.0.0.1", 8234);
try {
tv = (TextView) chat.findViewById(R.id.textView);
pw = new PrintWriter(client.getOutputStream());
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
new Thread(new Runnable() {
@Override
public void run() {
try {
// If i just try this, it works
tv.append("\n"+br.readLine());
// If I'm trying this, it crashes
String message = "";
while((message = br.readLine()) != null) {
tv.append("\n"+message);
}
} catch (IOException e) {
tv.append(e.getMessage());
}
}
}).start();
tv.append("Connected!");
pw.println(uuid+":B31TR1TT");
pw.flush();
} catch (IOException e) {
tv.append(e.getMessage());
}
} catch (IOException e) {
tv.append(e.getMessage());
}
}
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
尝试使用此代码代替 while 循环代码 或访问此 link http://hastebin.com/inecerakes.java
正如 algui91 所说,将网络调用移至单独的线程。严格模式错误 (violation=4) 表示 ui 线程上的网络调用。
只需将网络调用重构为后台任务(服务、asynctask 或其他),问题就会消失。
将 UI 与业务逻辑或网络通信分开。 test/debug.
总是更好更容易