I'm trying to track the progress of an upload using OkHttp. I have created a custom RequestBody
with the following body (courtesy of this answer) which writes to sink and publishes the progress.
public class CountingFileRequestBody extends RequestBody {
private static final String TAG = "CountingFileRequestBody";
private final ProgressListener listener;
private final String key;
private final MultipartBody multipartBody;
protected CountingSink mCountingSink;
public CountingFileRequestBody(MultipartBody multipartBody,
String key,
ProgressListener listener) {
this.multipartBody = multipartBody;
this.listener = listener;
this.key = key;
}
@Override
public long contentLength() throws IOException {
return multipartBody.contentLength();
}
@Override
public MediaType contentType() {
return multipartBody.contentType();
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
mCountingSink = new CountingSink(sink);
BufferedSink bufferedSink = Okio.buffer(mCountingSink);
multipartBody.writeTo(bufferedSink);
bufferedSink.flush();
}
public interface ProgressListener {
void transferred(String key, int num);
}
protected final class CountingSink extends ForwardingSink {
private long bytesWritten = 0;
public CountingSink(Sink delegate) {
super(delegate);
}
@Override
public void write(Buffer source, long byteCount) throws IOException {
bytesWritten += byteCount;
listener.transferred(key, (int) (100F * bytesWritten / contentLength()));
super.write(source, byteCount);
delegate().flush(); // I have added this line to manually flush the sink
}
}
}
The problem here is, the data is written to the sink immediately without actually sending the buffered bytes to the server. Meaning my progress reaches the end long before the actual upload.
Note: Some say that the sink needs to be flushed on every iteration for bytes to be actually uploaded but it's not working for me.