我使用HTTPURLConnection连接到服务器,我的响应包含Base64图像数据。当尝试使用getInputStream读取响应时,它不会读取完整的响应,会在两者之间中断。我的响应包含JSON格式的对象列表,每个对象都包含BASE64图像数据。尝试从第一个对象读取第一个图像数据时,读取中断。虽然它没有显示任何错误,但它显示了图像的一半,data.How我是否得到了完整的响应??以下是我的代码
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);发布于 2016-04-19 13:41:50
您尝试过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读取数据更容易、更快。
https://stackoverflow.com/questions/36709266
复制相似问题