0

I am using okhttp to send http form to server . this is my request:

OkHttpClient client = new OkHttpClient();     
RequestBody multiPartBody = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addPart(
                                Headers.of("Content-Disposition", "form-data; name=\"file0\"; filename=\"" + name.split("\\.")[0] + "\""),
                                RequestBody.create(MediaType.parse(mediaType), new File(path)))
                        .addFormDataPart("RadUAG_fileName", name)
                        .addFormDataPart("RadUAG_position", "0")
    Request request = new Request.Builder()
                        .url("http://xxx.xxx.xxx.xxx/ArchiveSite/Handlers/RadUploadHandler.ashx")
                        .post(multiPartBody)
                        .build();
    client.newCall(request).execute()

Now imagine my file has big size and i want to show progress to user that how much of upload is sent to server.

How can i get a mount of file that sent to server and show to user progress bar?

As you can see i am using okhttp and i want to find solution in this library.

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149

1 Answers1

0

I found this example to custom RequestBody .

So i create CountingFileRequestBody class:

public class CountingFileRequestBody extends RequestBody {

    private static final int SEGMENT_SIZE = 2048;
    private final File file;
    private final ProgressListener listener;
    private final String contentType;

    public CountingFileRequestBody(File file, String contentType, ProgressListener listener) {
        this.file = file;
        this.contentType = contentType;
        this.listener = listener;
    }

    @Override
    public long contentLength() {
        return file.length();
    }

    @Override
    public MediaType contentType() {
        return MediaType.parse(contentType);
    }

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        Source source = null;
        try {
            source = Okio.source(file);
            long total = 0;
            long read;

            while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) {
                total += read;
                sink.flush();
                this.listener.transferred(total);

            }
        } finally {
            Util.closeQuietly(source);
        }
    }

    public interface ProgressListener {
        void transferred(long num);
    }
}

And in main activity I use like this:

RequestBody multiPartBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addPart(
                        Headers.of("Content-Disposition", "form-data; name=\"file0\"; filename=\"" + name.split("\\.")[0] + "\""),
                        new CountingFileRequestBody(new File(path), mediaType, new CountingFileRequestBody.ProgressListener() {
                            @Override
                            public void transferred(long num) {
                                float progress = (num / (float) totalSize) * 100;
                                handler.post(() -> {
                                    mProgress.setProgress((int) progress);
                                    tvProgressPercentage.setText((int) progress + "%");
                                });
                            }
                        }))
                .addFormDataPart("RadUAG_fileName", name)

Now i have a progress to show to user :-)

enter image description here

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149