Skip to content Skip to sidebar Skip to footer

Http Get Request Is Missing Data

Hi i have a strange problem with HTTP in Android. I'm trying to get a picture from a remote server and display it on the device. If the picture is a small JPEG, this is not a probl

Solution 1:

available()

will return the number of bytes that con be read from the inputstream without blocking. So it will be return data that could be read from the network without blocking.

try with:

            InputStream bis = new InputStream(connection.getInputStream());
            Log.d("TEST","Length of Input " +bis.available());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int read = 0;
            byte[] buffer = newbyte[SIZE_OF_IMAGE];
            byte[] inBuff = newbyte[32678]
            while ((read = bis.read(inBuff, 0, 32768)) > 0) {
                   // copy in buffer what have been read// from the input stream
            }

            // close the is
            Bitmap bmp = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);

obviously is not there are more efficient way to do this but could be an entry point. Let me know if you need more help.

Edit: of course you have to avoid doing blocking call in the UI thread, as people have suggested.

Solution 2:

Thinks you need to Know bis.available() will always return less than 65Kb because Max IP Packet size is 65535 bytes. InputStream will return you the Total Size

As you have gave only one read Statement at any point of time data array will always be less than 65kb. (i.e. Data of first Packet)

What you can do is use BitmapFactory.decodeStream(InputStream is) and give the InputStream bis it will read all the bytes in the Stream and give you the Bitmap

EDITED

USE THIS FUNCTION

this will give you all the Bytes from InputStream

publicstatic byte[] ReadToEOF(InputStream stream) throws Exception
    {
        try {
            ByteArrayOutputStream array = new ByteArrayOutputStream();
            int read = 0;
            byte[] receivedData = new byte[5000];
            while((read = stream.read(receivedData)) > 0)
            {
                array.write(receivedData, 0, read);
                if(!(stream instanceof FileInputStream) && stream.available() == 0)
                {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    if(stream.available() == 0)
                        break;
                }
            }
            returnarray.toByteArray();
        } catch (Exception e) {
            // TODO: handle exceptionthrow e;
        }
    }

Thread.Sleep is because to get Next Packet through Internet there can be some delay

Post a Comment for "Http Get Request Is Missing Data"