相关疑难解决方法(0)

使用OKHTTP跟踪多部分文件上载的进度

我正在尝试实现一个进度条来指示多部分文件上载的进度.

我已经阅读了对这个答案的评论 - /sf/answers/1699994341/,我必须将传递给传递给RequestBody的接收器包装起来并提供跟踪移动的字节的回调.

我已经创建了一个自定义RequestBody并使用CustomSink类包装了接收器,但是通过调试我可以看到字节是由RealBufferedSink ln 44 编写的,而自定义接收器写入方法只运行一次,不允许我跟踪移动的字节数.

    private class CustomRequestBody extends RequestBody {

    MediaType contentType;
    byte[] content;

    private CustomRequestBody(final MediaType contentType, final byte[] content) {
        this.contentType = contentType;
        this.content = content;
    }

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

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

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        CustomSink customSink = new CustomSink(sink);
        customSink.write(content);

    }
}


private class CustomSink implements BufferedSink {

    private static final String TAG = "CUSTOM_SINK"; …
Run Code Online (Sandbox Code Playgroud)

android okhttp okio

35
推荐指数
2
解决办法
2万
查看次数

OKHTTP 3跟踪分段上传进度

如何在OkHttp 3中跟踪上传的进度我可以找到v2但不是v3的答案,就像这样

来自OkHttp配方的示例Multipart请求

private static final String IMGUR_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {
    // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("title", "Square Logo")
            .addFormDataPart("image", "logo-square.png",
                    RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
            .build();

    Request request = new Request.Builder()
            .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
            .url("https://api.imgur.com/3/image")
            .post(requestBody)
            .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code …
Run Code Online (Sandbox Code Playgroud)

android okhttp3

12
推荐指数
2
解决办法
8417
查看次数

标签 统计

android ×2

okhttp ×1

okhttp3 ×1

okio ×1