我正在使用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条答案
按热度按时间o7jaxewo1#
您可以尝试以下机制: