1

I am using HTTPURLConnection to connect to server and my response contains Base64 image data. When trying to read the response using getInputStream its not reading the complete response, breaks in between. My response contains list of objects in JSON format and each object contains BASE64 image data. Reading breaks while trying to read the first image data from the first object. Though its not showing any error it displays till half of the image data.How do i get the full response?? Here is my code

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);
Anoop LL
  • 1,548
  • 2
  • 21
  • 32
  • 2
    It is possible that since Base64 strings are long, it gets truncated when you try to log them. Try saving it in a file. You should be able to see the whole String. – Eric B. Apr 19 '16 at 05:27
  • @EricB. tried it but getting same result. – Anoop LL Apr 19 '16 at 05:28
  • 2
    try method from this link to get inputStream http://stackoverflow.com/questions/8654876/http-get-using-android-httpurlconnection – Vivek Mishra Apr 19 '16 at 05:29
  • @VivekMishra i tried that method. Now am getting some more data but again it fails to read. – Anoop LL Apr 19 '16 at 05:39

1 Answers1

0

Have you tried the example code from 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();
}

where readStream is your own method.

Reading from a BufferedInputStream is easier and faster.

totoro
  • 2,469
  • 2
  • 19
  • 23