public void connectAndRead(){
Socket socket = null;
try {
socket = new Socket("192.168.1.11", 88);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(64);
byte[] buffer = new byte[64];
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
String response1 = byteArrayOutputStream.toString("UTF-8");
String[] delims = response1.split("[,]");
byteArrayOutputStream.reset();
Double temp = Double.parseDouble(delims[0]);
Double humidity = Double.parseDouble(delims[1]);
String res = "Temp = " + temp + "Humidity = " + humidity;
result.setText(res);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我有工作服务器和结果函数result.setText(res)
但在socket = new Socket("192.168.1.11", 88)
之后
我“终于”没有任何例外。
错误在哪里?