Reputation: 219
I try to load file to server. And i need to wait before all operation will done for start enother method. So i use synchronous call. And i try to show ProgressDialog before start new thread, but, while all threads does not finished my UI thread just stuck.
private void uploadImageSync() {
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(BarcodActivity.this);
progressDialog.setMessage("Load...");
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 {
resultCall.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
}});
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
progressDialog.dismiss();
}
Upvotes: 1
Views: 221
Reputation: 2124
private class DownloadFileTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog = null;
private WeakReference<Activity> weakReference = null;
public DownloadFileTask(Activity activity) {
reference = new WeakReference<>(activity);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(weakReference.get());
progressDialog.setMessage("Load...");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
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);
resultCall.execute().body();
}
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
if(weakReference != null)
weakReference.clear();
weakReference = null;
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
progressDialog = null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(weakReference != null)
weakReference.clear();
weakReference = null;
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
progressDialog = null;
}
}
Upvotes: 0
Reputation: 37604
The reason why your UI Thread is freezing is because you are calling t.join()
. Your UI Thread
waits until the new Thread
is finished.
Instead you could make use of the AsyncTask
since that class was made for such a task.
Here is a general example on how to use it and the android dev guide
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
new DownloadFilesTask().execute(url1, url2, url3);
Note that the onPostExecute
runs on the UI Thread so you could easily dismiss it there.
Upvotes: 1
Reputation: 1033
You UI thread is get blocked in t.join()
line as https://docs.oracle.com/javase/tutorial/essential/concurrency/join.html mentioned:
The join method allows one thread to wait for the completion of another.
Just calling start
method is enough to run your thread.
Upvotes: 0