While the info is being consistently downloaded to the server, I want to draw the ProgressDialog, but all the activity sticks before the end of all threads.

private void uploadImageSync() { final ProgressDialog progressDialog; progressDialog = new ProgressDialog(BarcodActivity.this); progressDialog.setMessage("Загрузка фото..."); progressDialog.show(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ROOT_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); RequestInterface service = retrofit.create(RequestInterface.class); int i=0; while (i++ <= 4) { File f = getOutputMediaFilePath(mCode + "_"+i, true); if(f.exists()){ RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), f); MultipartBody.Part body = MultipartBody.Part.createFormData("file", f.getName(), requestFile); final Call<ResponseBody> resultCall = service.uploadImage(body); Thread t = new Thread(new Runnable() { public void run() { try { ResponseBody r = resultCall.execute().body(); Log.d("MyLog", "загружен"); } catch (IOException e) { e.printStackTrace(); } }}); t.start(); try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } progressDialog.dismiss(); } 
  • Why don't you use the Call interface from a retrofit? - pavel163
  • Yes, I have not really figured out the possibilities of retrofit. Isn't he Call<ResponseBody> - Bogdan Shulga
  • He, but you do not make a request through it - pavel163
  • Hmm ... what's that in the stream? resultCall.execute().body(); - Bogdan Shulga
  • one
    Its t.join(); You have blocked the current thread (in this case, the UI thread) for a time until each of the threads in the loop dies. Either the loop needs to be moved inside the stream, or as it is correctly suggested: run the following query in onResponse() . In addition, multipart can not send all files at once? - Yura Ivanov

1 answer 1

Try this

  Call<ResponseBody> result = service.uploadImage(body); result.enqueue(new Callback<Object>() { @Override public void onResponse(Call<ResponseBody> call, Response<Object> response) { progressDialog.hide(); } @Override public void onFailure(Call<Object> call, Throwable t) { progressDialog.hide(); } }); 
  • This is an asynchronous. And I have to consistently perform several requests - Bogdan Shulga
  • Well, you can run the following query on onResponse - pavel163
  • Doing recursion ... I had such an idea ... but it seemed like a lot of trouble. The current version seemed easier to me. If not to take into account problems with ProgressDialog - Bogdan Shulga
  • @BogdanShulga in the current version you do not solve the problem with the dialogue - temq