First, I have a class that extends AsyncTask. Then in the doInBackground() method, I have some code that posts an HTTP post message, and gets a response. This response is read into a string as follows:
DataInputStream dis = new DataInputStream(conn.getInputStream());
byte[] data = new byte[32768];
len = dis.read(data, 0, 32768);
if (len > 0)
{
response = new String(data, 0, len);
}
At this point, response should be 4127 bytes (as confirmed by the server). 1/5th of the time it IS 4127 bytes. 2/5 of the time it’s 1205 bytes, and 2/5 of the time it’s 3853 bytes. I can close the app, reinstall, and I get the same variation.
The person who manages the server logs everything and can confirm the same data is being sent over and over. But Android isn’t getting the full stream. This is wireless, not 3g. When I use the debugger, I can see that the string returned is always identical, just chopped off at a random spot.
Sorry for such a vague (stupid?) question, but I’ve been looking at this for hours and I can’t see what’s wrong with it…
The
readmethod documentation says: Reads up to len bytes of data from the contained input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.Depending on different conditions, sometimes you can read all server-sent data, sometimes there is a short-read. In order to get all data you should keep reading the stream in a loop until
lenis -1, indicating an EOF.