1

I am making a post to a web service from my android activity through AsyncTask inner class. My idea is that each time I post to the web service. I want the AsynTask to return a string value to the Activity, informing the activity if there was a successful post or a failure in posting to the webservice. I did this through a get method but I noticed when I click the button to post my UI would freeze for a while before responding. but the post still works. Please is there a way I can prevent this UI freezing. My code is below.

This is my doBackground method in my AsyncTask class

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        String result = "";

        PatientModel patient = new PatientModel();
        patient.setPhone(Phone);
        patient.setEmail(Email);        
        patient.setGCMRegistrationID(GCMRegistrationID);

        JSONHttpClient jsonHttpClient = new JSONHttpClient();
        patient = (PatientModel) jsonHttpClient.PostObject(RestfulServiceUrl.RegisterPatient, patient, PatientModel.class);
        GCMRegistrar.setRegisteredOnServer(context, true);

        if(patient != null) {

            result = patient.getPhone();
        }
        else if(patient == null){
            result = "failed";
        }

        return result;
    }

This is the script were I collect the values in my activity

try {                       
    String result = new RegisterPatient(RegisterActivity.this,resource,email,phone,regId).execute().get();
}
catch(Exception e){
}
mmBs
  • 8,421
  • 6
  • 38
  • 46
skliz4rel
  • 198
  • 2
  • 11
  • What are you looking to do with `result`? – karllindmark Sep 16 '13 at 09:53
  • Please post the rest of code, the doInBackground on AsyncTask will not block your ui Thread. – Sulfkain Sep 16 '13 at 10:01
  • I want to use the result to know if there was a successful post to the webservice or there was a failure. There a conditions were there is poor internet on a users phone, there would be a failure in post. So the result would help me to give the user friendly message to users. – skliz4rel Sep 16 '13 at 10:01

2 Answers2

1

You should never use get() method when implementing an AsyncTask, since it turns an asynchronous call into a synchronous one. Usually the result retrieval is implemented using a custom listener interface, which is implemented by the object which should receive the response.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • Please could you give me an example cos I dont know how to do this. A simple one. I agree with you cos I have done some research and seen ur suggestions. But dont know how to solve the problem know without a get() method. Thanks for your response. – skliz4rel Sep 16 '13 at 10:20
  • @user2754532, Check this answer, it contains an example of implementation: http://stackoverflow.com/questions/13815807/return-value-from-asynctask-class-onpostexecute-method – Egor Sep 16 '13 at 10:55
  • Thanks alot, Egor, This works like magic. I am very very very very very very grateful. You are the bomb. You made my day. – skliz4rel Sep 16 '13 at 11:53
0

get() should never be called from asynctask's doInBackgroundMethod().In an AsyncTask you get 3 utility methods.

onPreExecute(),doInBackGround(),and onPostexecute(). onPreExecute() and onPostexecute() runs on UI thread and doInBackGround() runs in a separate background thread. If you want to pass your value which you are getting in the doInBackGround() method to the activity, pass the referece of the activity to the asynctask. When you return a value from doInBackGround() ,the onPostexecute() method captures that value . you can pass the specific value to the activity by calling any method from the activity in the onpostExecute() method. Also I think the use of a interface is the best in this case.

Ritaban
  • 763
  • 4
  • 8