场景:后端上传文件接口只支持单个文件上传,而业务需求一次性上传多个图片,因此需要多个上传任务并发进行,拿到所有的返回结果后,才能进行下一个流程。
private List<Response<JSONObject>> responses = new ArrayList<>();
private int requestCount = 0;
private void submitTest() {
for (LocalMedia localMedia : mSelectList) {
uploadImage(new File(localMedia.getPath()));
}
}
private void uploadImage(File file) {
JSONObject data = new JSONObject();
data.put("file",file);
RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), imageBody);
HttpApi api= RetrofitManager.initRetrofit(RetrofitManager.url_fms).create(HttpApi.class);
Call<JSONObject> resultcall = api.up_file("e2e5198e198c78cb59cebfadc592aa45", part);
resultcall.enqueue(new Callback<JSONObject>() {
//请求成功时回调
@Override
public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
if(response.isSuccessful()){
JSONObject json = response.body();
if(json.getBoolean("flag")){
responses.add(response);
handleResponse();
}else{
XToastUtils.error(json.getString("message"));
}
}else {
XToastUtils.error(response.message()+"");
}
}
//请求失败时候的回调
@Override
public void onFailure(Call<JSONObject> call, Throwable throwable) {
XToastUtils.error(throwable.getMessage()+"");
}
});
}
private synchronized void handleResponse() {
requestCount++;
if (requestCount == mSelectList.size()) {
// 所有请求都已完成,可以在这里处理合并的结果
handleCombinedResponse(responses);
}
}
/**
* 处理合并的结果
*/
private void handleCombinedResponse(List<Response<JSONObject>> responses) {
List<String> result = new ArrayList<>();
for (Response<JSONObject> response : responses){
if(response.isSuccessful()){
JSONObject json = response.body();
if(json.getBoolean("flag")){
XToastUtils.success(json.getString("message"));
result.add(json.getString("data"));
}else{
XToastUtils.error(json.getString("message"));
}
}else {
XToastUtils.error(response.message()+"");
}
}
System.out.println(result);
}
以上使用了synchronized关键字来确保在多线程环境中handleResponse()方法能够正确地计数并处理响应。
运行结果:
public String performParallelRequests() {
List<CompletableFuture<String>> futures = new ArrayList<>();
for (LocalMedia localMedia : mSelectList) {
File file = new File(localMedia.getPath());
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
JSONObject data = new JSONObject();
data.put("file",file);
RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), imageBody);
HttpApi api= RetrofitManager.initRetrofit(RetrofitManager.url_fms).create(HttpApi.class);
Call<JSONObject> resultcall = api.up_file("e2e5198e198c78cb59cebfadc592aa45", part);
Response<JSONObject> execute = resultcall.execute();
if(execute.isSuccessful()){
return execute.body().getString("data");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
});
futures.add(future);
}
List<String> results = new ArrayList<>();
CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
try {
allOf.get();
for (CompletableFuture<String> future : futures) {
String result = future.get();
results.add(result);
System.out.println(result);
}
System.out.println("results---------"+results);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return null;
}
运行结果: