我使用HTTPURLConnection
连接到服务器,我的回复包含Base64
图像数据。当尝试使用getInputStream
读取响应时,它没有读取完整的响应,介于两者之间。我的响应包含JSON格式的对象列表,每个对象包含BASE64图像数据。尝试从第一个对象读取第一个图像数据时读取中断。虽然它没有显示任何错误,但它会显示到图像数据的一半。我如何得到完整的响应?这是我的代码
InputStream is = httpURLConnection.getInputStream();
byte[] b = new byte[1024];
StringBuffer buffer=new StringBuffer();
while ( is.read(b) != -1){
buffer.append(new String(b));
System.out.println("Read= "+is.read());
}
System.out.println(buffer);
答案 0 :(得分:0)
您是否尝试过Google的示例代码?
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
其中readStream
是您自己的方法。
从BufferedInputStream
阅读更容易,更快。