private void readIncomingMessage() {
try {
StringBuilder builder = new StringBuilder();
InputStream is = socket.getInputStream();
int length = 1024;
byte[] array = new byte[length];
int n = 0;
while ((n = is.read(array, n, 100)) != -1) {
builder.append(new String(array));
if (checkIfComplete(builder.toString())) {
buildListItems(builder.toString(), null);
builder = new StringBuilder();
}
}
} catch (IOException e) {
Log.e("TCPclient", "Something went wrong while reading the socket");
}
}
我想读每100个字节块流,这些字节转换为字符串,比看看是否有适合的字符串一定的条件。
但是,当我调试时,我看到该构建器的计数为3072.
而且我看到一个像(text,,,,,,,text,,,,,,text)
我该如何将文本添加到stringbuilder中?
THX :)
private void readIncomingMessage() {
try {
StringBuilder builder = new StringBuilder();
InputStream is = socket.getInputStream();
int length = 100;
byte[] array = new byte[length];
int n = 0;
while ((n = is.read(array, 0, length)) != -1) {
builder.append(new String(array, 0, n));
if (checkIfComplete(builder.toString())) {
buildListItems(builder.toString(), null);
builder = new StringBuilder();
}
}
} catch (IOException e) {
Log.e("TCPclient", "Something went wrong while reading the socket");
}
}
该解决方案并获得成功对我来说。 此解决方案的任何缺点?
这并不一定是连续的100个字节的任何特定块将成功地转化为'String'的情况下... – 2012-07-12 15:04:12