美好的一天。有人可以幫助我嗎?我似乎無法找到解決此問題的方法。我不知道爲什麼當我關閉InputStream和套接字時出現「無法訪問的代碼」錯誤。給「無法訪問的代碼」錯誤
感謝您的幫助。
public void run() {
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream;
while (true) {
inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
textResponse.setText(response);
if(response == "Server: You are connected to Android Server"){
Log.v("ok","ppp");
}
if(response == "OK"){
Log.v("ok","ooo");
}
}
inputStream.close(); --------unreachable code
socket.close(); --------unreachable code
} catch (UnknownHostException e) {
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
e.printStackTrace();
response = "IOException: " + e.toString();
}
}
控制不能從無限循環中出來,因此在某些情況下需要添加一些條件或循環。 – 2014-09-23 02:30:58
'while(true)'是一個無限循環。 – 2014-09-23 04:45:05