0

I want to use progress dialog with asynctask how could I use that.I tried but it showing me the dialog after getting the response .this is my calling function..to the asycnk task As I am expecting the issue I am facing is because of this AppResponse = reqClient.execute().get(); Because I have to get the return value also from this Async task .So please see both the files for refrence

 void postHttpRequest(String userId,String pass,TextView error){
            RequestClient reqClient = new RequestClient(IweenTravelLoginPage.this);
            String AppResponse = null;
            try {
                url = "";
                Log.d("Http Post URL is ", url);
                AppResponse = reqClient.execute().get();

                String status = ValidateLoginStatus.checkLoginStatus(AppResponse);
                Log.d("Status recived", status);

                if(status.equals("200")){
                    saveInformation(userId,pass);
                    startingActivity();
                }else{
                    error.setText("Incorrect UserName or Password");
                }
            } catch (Exception e) {
                Log.e("Exception Occured", "Exception is "+e.getMessage());
            }

This function is for sending the request

public class RequestClient extends AsyncTask<String, Void, String>{
    Context context;

    public RequestClient(Context c) {
        context = c;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... aurl){
    String responseString="";
    HttpClient client = null;
    try {
         client = new DefaultHttpClient();  
         HttpGet get = new HttpGet(IweenTravelLoginPage.url);
         HttpResponse responseGet = client.execute(get);  
         HttpEntity resEntityGet = responseGet.getEntity();  
         if (resEntityGet != null) {  
             responseString = EntityUtils.toString(resEntityGet);
             Log.i("GET RESPONSE", responseString.trim());
         }
    } catch (Exception e) {
        Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
    }
        Log.d("ANDRO_ASYNC_RESPONSE", responseString.trim());
        client.getConnectionManager().shutdown();
     return responseString.trim();

    }


    @Override
    protected void onPostExecute(String response) {
         super.onPostExecute(response); 
        }
}

So please suggest me what I have to do to show the process dialog.I tried all the example on the stackoverflow .But that all are not working for me.Please help

  • have you tried showing it in onPreExecuted and dismiss it inside onPostExecute? – Blackbelt Jul 23 '13 at 07:23
  • @Gaurav Pandey check this http://stackoverflow.com/questions/17585759/cant-dismiss-progressdialog-after-the-asynctask-complete/17585792#17585792 – TheFlash Jul 23 '13 at 07:23
  • @Pratik I already tried that but not working it might possible some issues in my code can u suggest me the changes in my code –  Jul 23 '13 at 07:25
  • @Pratik I think the issue is from this AppResponse = reqClient.execute().get(); –  Jul 23 '13 at 07:29

4 Answers4

1

You can try this one

extends AsyncTask<String, Void, String>{
Context context;

public RequestClient(Context c) {
    context = c;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog = new ProgressDialog(context);
    progressDialog.setMessage("Loading. Please Wait");
    progressDialog.setIndeterminate(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setCancelable(true);
    progressDialog.show();
}

@Override
protected String doInBackground(String... aurl){
String responseString="";
HttpClient client = null;
try {
     client = new DefaultHttpClient();  
     HttpGet get = new HttpGet(IweenTravelLoginPage.url);
     HttpResponse responseGet = client.execute(get);  
     HttpEntity resEntityGet = responseGet.getEntity();  
     if (resEntityGet != null) {  
         responseString = EntityUtils.toString(resEntityGet);
         Log.i("GET RESPONSE", responseString.trim());
     }
} catch (Exception e) {
    Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
}
    Log.d("ANDRO_ASYNC_RESPONSE", responseString.trim());
    client.getConnectionManager().shutdown();
 return responseString.trim();

}


@Override
protected void onPostExecute(String response) {
     super.onPostExecute(response); 
     progressDialog.dismiss();
        }
}
Kartheek Sarabu
  • 3,886
  • 8
  • 33
  • 66
1

just building on previous answers, you can get the data returned from the Async task by modifying the listener function to take in a parameter which is the return data

public interface OnTaskCompleted{
    void onTaskCompleted(String response);
}

Then in the postExecute function of the Async task have it call the listener with the return data

    @Override
    protected void onPostExecute(String response){
        //your stuff
        listener.onTaskCompleted(response);
    }

Then where you call the Async task you just need to implement the listener. you can either have a class implement the listener or use an anonymous class

OnTaskCompleted listener = new OnTaskCompleted() {
    void onTaskCompleted(String response){
        // We got return data from the Async Task, do stuff!
    }
};
RequestClient reqClient = new RequestClient(listener);
reqClient.execute();

In order to show a progress dialog you should set it to display before you call the async task, and then once you have received the call back from the Aysnc task you can dismiss it.

//make progressDialog a member of your class
progressDialog = new ProgressDialog(IweenTravelLoginPage.this);
progressDialog.setMessage("Loading");
progressDialog.show();

OnTaskCompleted listener = new OnTaskCompleted() {
    void onTaskCompleted(String response){
        // get rid of the progress dialog
        progressDialog.dismiss;
        // We got return data from the Async Task, do stuff!
    }
};
RequestClient reqClient = new RequestClient(listener);
reqClient.execute();
Jamie Redman
  • 431
  • 2
  • 8
  • these answers are quite complicated for me even i am not able to understand them what changes i have to make in my code.i am new android .if u can help me in my code i will be thank full to u. –  Jul 23 '13 at 11:44
  • 1
    Essentially the interface can either belong in its own file. The onPostExecute piece replaces the post execute in your AsyncTask. Then the last piece goes into your postHttpRequest function. The only peice I really left out was modifying the constructor of AsyncTask to take in an instance of your listener – Jamie Redman Jul 23 '13 at 11:54
  • I updated my code as suggested by u Please suggest now how could i use processDialog in it –  Jul 24 '13 at 07:32
  • I edited my answer, to include the progress dialog. Let me know if that works for you – Jamie Redman Jul 24 '13 at 16:35
0

For this purpose, you can use onProgressUpdate() in your asyncTask.. Which is called everyTime you do showProgressUpdate() from doInBackground()

Abhishek Shukla
  • 1,242
  • 8
  • 11
0

Can look this code.this can be simple example

public class LoginSync extends
            AsyncTask<ArrayList<NameValuePair>, Void, UserVO> {

        @Override
        protected UserVO doInBackground(ArrayList<NameValuePair>... params) {
            // TODO Auto-generated method stub
            return WebServiceClient.SendHttpPost("/us/si",
                    params[0], Use.class);
        }

        @Override
        protected void onPostExecute(UserVO result) {
            // TODO Auto-generated method stub
            if (result == null) {
                progressLayout.setVisibility(View.GONE);
                Toast.makeText(getApplicationContext(),
                        "Bağlantı Sorunu Tekrar Deneyiniz. ", 500).show();
                return;
            }
            UserVO userVO = new UserVO();
            userVO.setUserName(result.getUserName());
            userName = result.getUserIdentificationVO().getFirstName();
            userSName = result.getUserIdentificationVO().getLastName();



            progressLayout.setVisibility(View.GONE);

        }

    }
celebi
  • 21
  • 4