使用OkHTTP android发送多个请求

时间:2018-01-26 19:28:05

标签: java android okhttp

我使用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();

1 个答案:

答案 0 :(得分:1)

您可以尝试以下机制:

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
}