Here inside background method need to call multiple API request based on the size of the list. But i'm using while(). It is not wait un till request API and get response. So am using thread inside. How to synchronized. Once the API call get success after only need to call next request.
private class AsyncPost extends AsyncTask<String, Void, Void> {
String strResult = "";
@Override
protected Void doInBackground(String... params) {
try {
GlobalVariables.rqCount=0;
mHandler=null;
mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(android.os.Message message) {
onProgressUpdate(message.getData().getString("MessageString"));
}
};
strResult = "ENSURE NOT EMPTY";
strResult=getRequestResult(Listactivity.this,50, mHandler);
}
catch (Exception e)
{
dialog.dismiss();
}
return null;
}
@Override
protected void onPostExecute(Void resultGetlogin) {
try {
dialog.dismiss();
if(strResult.equals("Success")){
CustomDiallog.showAlertDialog(Listactivity.this, "Successfully!!");
}else{
CustomDiallog.showAlertDialog(Listactivity.this, "FAIL!!");
}
} catch (Exception e) {
dialog.dismiss();
}
}
protected void onProgressUpdate(String... values) {
dialog.setMessage(""+values[0]);
}
protected void onPreExecute() {
dialog = ProgressDialog.show(Sync_Bulk.this, "", "");
}
}
This thread am using but getting crash help me
private class getRequestResult(Context context, int allSyncLimit,Handler handler2){
new Thread(new Runnable() {
@Override
public void run() {
try {
inalContext.wait();
if (res_result.equals("Success")) {
result[0] = "Success";
} else {
result[0] = "fail";
}
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (finalContext) {
postRequest((Activity) finalContext, list); // This is Api method and notify() is called
}
}
}).start();
}
}
You need something similar to:
Just call "doAllRequests(...)" providing the appropriate List. A single "OnPreExecute()"-like will be executed at first API call and a single "OnPostExecute()"-like will be executed at the end of last APi call. At each API Call (success or fail) "case 111:" will be called from the Main/UIThread.