【发布时间】:2011-04-16 21:29:15
【问题描述】:
我想重用来自 HTTP response.resultset() 的 Inputstream...
- 我将 inputstream 转换为 byte[] 数组(也需要存储,所以我创建了 byte 数组。)
- 比转成字符串
- 当我将它传递给 document.parse(string) 时出现错误 saxparserException:---eof not found
- 使用 document.parse(stream) 工作正常。
//------------以下方法对我没有帮助,每种方法都会导致
未找到 saxparserException 协议。
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
public static String readInputStreamAsString(InputStream in)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
byte b = (byte)result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}
//--------------
byte[] bytes=new byte[inputStream.available()];
inputStream.read(bytes);
String s = new String(bytes);
//------------------
StringBuilder response = new StringBuilder();
int value = 0;
boolean active = true;
while (active) {
value = is.read();
if (value == -1) {
throw new IOException("End of Stream");
} else if (value != '\n') {
response.append((char) value);
continue;
} else {
active = false;
}
}
return response.toString();
//--------------
BufferedInputStream ib = new BufferedInputStream(is,1024*1024);
StringBuffer sb = new StringBuffer();
String temp = ib.readLine();
while (temp !=null){
sb.append (temp);
sb.append ("\n");
temp = ib.readLine();
}
s = sb.toString()
【问题讨论】:
标签: java android document inputstream