I am trying downlaodFile from Api Services so I am using Retrofit library,Below codes working but works runs as a asyncrounously,it starts to download all file at the same time,I want to download files as a sycnronously when one request(download process) is completed and response comes,other request(download process) will work, how can I do this? and how to handle finally all request is completed
for (int i = 0; i < urlList.size; i++){
downloadFile(headerData,context,urlList.get(i),fileNameList.get(i),fileType.get(i));
}
downloadFile.class
public static void downloadFile(String headerData,Context context,String url,String fileName,String fileType){
RestInterface restInterface = RetroClient.getClientFile(url).create(RestInterface.class);
Call<ResponseBody> call = restInterface.doGetFile(headerData);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.isSuccessful()){
Log.d("Downloading FileResult:","" + response.body());
boolean success =
Utils.writeResponseBodyToDisk(response.body(),context,fileName,fileType);
if(success == true){
startManager(context);
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("TAG", "error" + t.getMessage());
}
});
}
RestInterfac class
public interface RestInterface {
@GET(".")
Call<ResponseBody> doGetFile(@Header("Authorization") String authHeader);
}
RetroClient class
public class RetroClient{
public static Retrofit getClientFile(String url) {
String BaseUrl = url + "/";
Gson gson = new GsonBuilder()
.setLenient()
.create();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
return new Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
}
}
Sncronously with execute method
private static class DownloadFileTask extends AsyncTask<String,Void,String>{
String Url,Header,FileName,FileType;
Context con;
public DownloadFileTask(String header,Context context,String url,String FileName,String FileType){
Url = url;
Header = header;
con = context;
this.FileName = FileName;
this.FileType = FileType;
}
@Override
protected String doInBackground(String... strings) {
RestInterface restInterface = RetroClient.getClientFile(Url).create(RestInterface.class);
Call<ResponseBody> call = restInterface.doGetFile(Header);
try{
Response<ResponseBody> response = call.execute();
ResponseBody responseBody = response.body();
Log.d("Downloading FileResult:","" + responseBody);
if(response.isSuccessful()){
boolean success = Utils.writeResponseBodyToDisk(response.body(),con,FileName,FileType);
/* if(success == true){
startManager(context);
}*/
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
calling Download File
public static void downloadFile(String headerData,Context context,String url,String fileName,String fileType){
new DownloadFileTask(headerData,context,url,fileName,fileType).execute();
}