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