I’m really despairing on calculating the moving average of an upload (binary/jpg) with http-post in my android app.
I am using DataOutputStream as ouputStream:
while (true) {
while(bufferSize > 0) {
int transferedBytes = Math.min(bufferSize, packetSize);
outputStream.write(buffer, offset, transferedBytes);
outputStream.flush();
// save transferedBytes as throughput
offset += transferedBytes;
bufferSize -= transferedBytes;
}
if ((available = inputStream.available()) <= 0) {
break;
}
int nBytes = Math.min(avail, bufferSize);
bufferSize = inputStream.read(buffer, 0, nBytes);
offset = 0;
}
The upload is working fine with this implemention, but the mentioned lines are executed in less than 500ms (file is 1MB). The real upload seems to be executed in the following line (takes about 11000ms):
int responseCode = connection.getResponseCode();
It seems, that I can’t solve my issue with the code above. Is there any approach which could do what I want? I’ve heard Apaches HttpCore has abilities to do stuff like that, but I couldn’t find any method or documentation which seems to help.
Does anyone has an idea how to do this?
Please make sure you use
setChunkedStreamingMode(int)otherwise the response body will be buffered in memory until you try to close the connection and get the response code back which seems like what you are experiencing.Android doc on setChunkedStreamingMode