Skip to content

Commit

Permalink
Merge pull request amitshekhariitbhu#158 from dg76/master
Browse files Browse the repository at this point in the history
Added support for displaying the upload progress of POST, PUT and PAT…
  • Loading branch information
amitshekhariitbhu authored Dec 19, 2017
2 parents 100e362 + f6bd2ce commit 5da24f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import com.androidnetworking.common.ANConstants;
import com.androidnetworking.common.ANRequest;
import com.androidnetworking.common.ConnectionClassManager;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interceptors.HttpLoggingInterceptor;
import com.androidnetworking.interceptors.HttpLoggingInterceptor.Level;
import com.androidnetworking.error.ANError;
import com.androidnetworking.utils.Utils;

import java.io.File;
Expand Down Expand Up @@ -73,12 +73,12 @@ public static Response performSimpleRequest(ANRequest request) throws ANError {
break;
}
case POST: {
requestBody = request.getRequestBody();
requestBody = new RequestProgressBody(request.getRequestBody(), request.getUploadProgressListener());
builder = builder.post(requestBody);
break;
}
case PUT: {
requestBody = request.getRequestBody();
requestBody = new RequestProgressBody(request.getRequestBody(), request.getUploadProgressListener());
builder = builder.put(requestBody);
break;
}
Expand All @@ -92,7 +92,7 @@ public static Response performSimpleRequest(ANRequest request) throws ANError {
break;
}
case PATCH: {
requestBody = request.getRequestBody();
requestBody = new RequestProgressBody(request.getRequestBody(), request.getUploadProgressListener());
builder = builder.patch(requestBody);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,26 @@ public void writeTo(BufferedSink sink) throws IOException {
if (bufferedSink == null) {
bufferedSink = Okio.buffer(sink(sink));
}
requestBody.writeTo(bufferedSink);

if (uploadProgressHandler != null) {
Buffer source = new Buffer();
requestBody.writeTo(source.emit());
long contentLength = contentLength();
long bytesWritten = 0;
long read;
while ((read = source.read(bufferedSink.buffer(), 2048)) != -1) {
bytesWritten += read;
sink.flush();

if (uploadProgressHandler != null) {
uploadProgressHandler.obtainMessage(ANConstants.UPDATE,
new Progress(bytesWritten, contentLength)).sendToTarget();
}
}
} else {
requestBody.writeTo(bufferedSink);
}

bufferedSink.flush();
}

Expand Down

0 comments on commit 5da24f6

Please sign in to comment.