android - How to return the result returning from asynctask on background -


i running task in ground, , returning result out of it.the result coming null returned before async task completes.how can resolve it

public result callserver(string zobjectnamep, string zmethodnamep, string querystringp) {  aresultm=new result(); mainaynsctask asynctask = new mainaynsctask(); try { asynctask.execute(zobjectnamep,zmethodnamep,querystringp); } catch(exception ex) {  } return aresultm; } 

the 4 steps when asynchronous task executed, task goes through 4 steps:

  1. onpreexecute(), invoked on ui thread before task executed. step used setup task, instance showing progress bar in user interface.

  2. doinbackground(params...), invoked on background thread after onpreexecute() finishes executing. step used perform background computation can take long time. parameters of asynchronous task passed step. result of computation must returned step , passed last step. step can use publishprogress(progress...) publish 1 or more units of progress. these values published on ui thread, in onprogressupdate(progress...) step.

  3. onprogressupdate(progress...), invoked on ui thread after call publishprogress(progress...). timing of execution undefined. method used display form of progress in user interface while background computation still executing. instance, can used animate progress bar or show logs in text field.

  4. onpostexecute(result), invoked on ui thread after background computation finishes. result of background computation passed step parameter.

use handler

in activity

mhandler = new handler() {  @override public void handlemessage(message msg) {    string s=(string)msg.obj;   tv.settext("result = "+s);  } };     

in onpostexecute

protected void onpostexecute(string result)//result returned in doinbackground { pd.dismiss(); if(result != null)  {  message msg=new message();  msg.obj=result.tostring();  mhandler.sendmessage(msg);  } } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -