android - OnPostExecute() of asynctask is not called first time -
i using asynctask in application. first time onpostexecute() not getting called. after next time calling. can problem?
this how invoke asynctask -
new notificationlistasynctask(this, notificationcounthandler).execute();
and below asynctask -
import java.io.ioexception; import java.io.unsupportedencodingexception; import org.apache.http.client.clientprotocolexception; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.content.context; import android.content.sharedpreferences; public class notificationlistasynctask extends asynctask<string, void, void> { context context; private static final string tag_data = "data"; private handler handler; public static final string key_notification_count = "key_notification_count"; public static final int key_notification_message = 1001; private static final string tag_notification_id = "id"; public notificationlistasynctask(context context, handler handler) { this.context = context; this.handler = handler; } @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(string... params) { // creating service handler class instance requesthandler requesthandler = new requesthandler(context); urlbuilder builder =appservice .getappservice().geturlbuilder(); // making request url , getting response string jsonstr = null; try { jsonstr = requesthandler.makeservicecall( builder.getnotificationlisturl( commonfunctions.getuserid(context), commonfunctions.getutctime(context)), requesthandler.get); } catch (unsupportedencodingexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (clientprotocolexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); jsonarray contents = null; // getting json array node jsonarray contentsenclosed = jsonobj.getjsonarray(tag_data); contents = contentsenclosed.getjsonarray(0); (int = 0; < contents.length(); i++) { jsonobject obj = contents.getjsonobject(i); string jsonvalue = obj.tostring(); string contentid = obj.getstring(tag_notification_id); appservice.getappservice().insertnotification(jsonvalue, contentid); } commonfunctions.setnotificationrequesttime(context); } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("requesthandler", "couldn't data url"); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); handler.sendemptymessage(key_notification_message); } }
thanks in advance.
please refer asynktask life cycle:
onpreexecute()
, invoked on ui thread before task executed. step used setup task, instance showing progress bar in user interface.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.
3 . onpostexecute(result)
, invoked on ui thread after background computation finishes. result of background computation passed step parameter.
so once doinbackground completed after onpostexecute method call.
please refer this document
Comments
Post a Comment