使用OkHTTP android发送多个请求

b09cbbtk  于 2022-12-25  发布在  Android
关注(0)|答案(1)|浏览(280)

我正在使用OkHTTP发送多个图像。由于所有的请求都是异步的,所以在所有请求完成之前,我无法运行最后的调用。如何在OkHTTP中实现这一点?
我需要在发送完所有图像后运行我的RunFinalRequest()。我尝试的是,我把我的RunFinalRequest()放在OnResponse()上,但有时即使有响应也无法通过。有更好的方法吗?
下面是我的代码:

public void SendALL(){

    OkHttpClient client = new OkHttpClient();
    client.dispatcher().setMaxRequestsPerHost(50);

    final MediaType MEDIA_TYPE = MediaType.parse("image/png");

    for (int i = 0; i < images.size(); i++){

        File sourceFile = new File(images.get(i).getImageURI());

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)

                .addFormDataPart("image", sourceFile.getName(), RequestBody.create(MEDIA_TYPE, sourceFile))
                .addFormDataPart("name", images.get(i).getJobID())
                .build();

        Request request = new Request.Builder()
                .url("xxxxxxxxxxx")
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }

    if(signatures.size() > 0){

        File sourceFile = new File(Uri.parse(signatures.get(0).getImageURI()).toString());

        String newJobID = signatures.get(0).getJobID().concat("-signature");

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)

                .addFormDataPart("image", sourceFile.getName(), RequestBody.create(MEDIA_TYPE, sourceFile))
                .addFormDataPart("name", newJobID)
                .build();

        Request request = new Request.Builder()
                .url("XXXXXXXXXXX")
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });

    }

RunFinalRequest();
o7jaxewo

o7jaxewo1#

您可以尝试以下机制:

private int uploadedImageCount;

...

public void sendAll() {
    uploadedImageCount = 0;
    OkHttpClient client = new OkHttpClient();
    client.dispatcher().setMaxRequestsPerHost(50);
    MediaType MEDIA_TYPE = MediaType.parse("image/png");

    for (int i = 0; i < images.size(); i++) {
        File sourceFile = new File(images.get(i).getImageURI());

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("image", sourceFile.getName(), RequestBody.create(MEDIA_TYPE, sourceFile))
                .addFormDataPart("name", images.get(i).getJobID())
                .build();

        Request request = new Request.Builder()
                .url("xxxxxxxxxxx")
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                onImageUploadFailed();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    onImageUploaded();
                } else {
                    onImageUploadFailed();
                }
            }
        });
    }

    if (signatures.size() > 0) {
        File sourceFile = new File(Uri.parse(signatures.get(0).getImageURI()).toString());
        String newJobID = signatures.get(0).getJobID().concat("-signature");

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("image", sourceFile.getName(), RequestBody.create(MEDIA_TYPE, sourceFile))
                .addFormDataPart("name", newJobID)
                .build();

        Request request = new Request.Builder()
                .url("XXXXXXXXXXX")
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }
}

public void onImageUploaded() {
    uploadedImageCount++;
    if (uploadedImageCount == images.size()) {
        runFinalRequest();
    }
}

public void onImageUploadFailed() {
    // Handle failure case. You may detect failed image and try to resend
}

相关问题