java - How to add asyncTask code in application? -
i have register activity in application. has inputs of userid,email,password , mobile no. have created ui.
code:
public class registeractivity extends appcompatactivity { textview already; button signup; relativelayout parent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_register); parent = (relativelayout)findviewbyid(r.id.parentpanel); setupui(parent); = (textview)findviewbyid(r.id.alreadyregistered); signup = (button) findviewbyid(r.id.sign_up_button); already.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivity(new intent(registeractivity.this,loginactivity.class)); } }); signup.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivity(new intent(registeractivity.this,loginactivity.class)); } }); } public static void hidesoftkeyboard(activity activity) { inputmethodmanager inputmethodmanager = (inputmethodmanager) activity.getsystemservice(activity.input_method_service); inputmethodmanager.hidesoftinputfromwindow(activity.getcurrentfocus().getwindowtoken(), 0); } public void setupui(view view) { //set touch listener non-text box views hide keyboard. if(!(view instanceof edittext)) { view.setontouchlistener(new view.ontouchlistener() { public boolean ontouch(view v, motionevent event) { hidesoftkeyboard(registeractivity.this); return false; } }); } //if layout container, iterate on children , seed recursion. if (view instanceof viewgroup) { (int = 0; < ((viewgroup) view).getchildcount(); i++) { view innerview = ((viewgroup) view).getchildat(i); setupui(innerview); } } } }
now want sync ui server.
for have code of asynctask created in activity. how can call code or implement code ui?
asynctask code : registeractivity
public class registeractivity extends appcompatactivity { context context; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_register); context = this; registerasynctask task = new registerasynctask(); string userphoto = "ivborw0kggoaaaansuheugaaaeaaaabacayaaacqaxheaaaabhncsvqicagifahkiaaaaalwsfllbaihagdimrn7hh1jkkmzz+d7mpu15md6ptcyrhmqvsgnvjy7djh69ogweau1pkvwankk0nlssgva8vk="; hashmap<string, string> params = new hashmap<string, string>(); params.put("userusername", "user1"); params.put("userpassword", "user1"); params.put("gender", "m"); params.put("birthdate", "1986/7/12"); params.put("religion", "hindu"); params.put("nationality", "indian"); params.put("mothertongue", "marathi"); params.put("birthplace", "pune"); params.put("usercountry", "india"); params.put("userstate", "maharashtra"); params.put("usercity", "nashik"); params.put("userpincode", "422101"); params.put("useremailid", "user1@gmail.com"); params.put("usermobileno", "9696323252"); params.put("userphoto", userphoto); } public class registerasynctask extends asynctask<map<string, string>, void, jsonobject>{ @override protected jsonobject doinbackground(map<string, string>... params) { try { string api = context.getresources().getstring(r.string.server_url) + "api/user/register.php"; map2json mjs = new map2json(); jsonobject jsonparams = mjs.getjson(params[0]); serverrequest request = new serverrequest(api, jsonparams); return request.sendrequest(); } catch(jsonexception je) { return excpetion2json.getjson(je); } } @override protected void onpostexecute(jsonobject jsonobject) { super.onpostexecute(jsonobject); log.d("serverresponse", jsonobject.tostring()); try { int result = jsonobject.getint("result"); string message = jsonobject.getstring("message"); if ( result == 1 ) { toast.maketext(context, message, toast.length_long).show(); //code having successful result register api goes here } else { toast.maketext(context, message, toast.length_long).show(); //code when api fails goes here } } catch(jsonexception je) { je.printstacktrace(); toast.maketext(context, je.getmessage(), toast.length_long).show(); } } } }
how can sync this? please help. thank you.
edit:
geteventsasynctask:
public class geteventsasynctask extends asynctask<void, void, jsonobject> { string api; private context context; public geteventsasynctask(context context) { this.context = context; } @override protected jsonobject doinbackground(void... params) { try { api = context.getresources().getstring(r.string.server_url) + "api/event/getevents.php"; serverrequest request = new serverrequest(api); return request.sendgetrequest(); } catch(exception e) { return excpetion2json.getjson(e); } } //end of doinbackground @override protected void onpostexecute(jsonobject response) { super.onpostexecute(response); log.e("serverresponse", response.tostring()); try { int result = response.getint("result"); string message = response.getstring("message"); if (result == 1 ) { toast.maketext(context, message, toast.length_long).show(); //code after getting profile details goes here } else { toast.maketext(context, message, toast.length_long).show(); //code after failed getting profile details goes here } } catch(jsonexception je) { je.printstacktrace(); toast.maketext(context, je.getmessage(), toast.length_long).show(); } } //end of onpostexecute }
dialog :
@override protected dialog oncreatedialog(int id) { dialog dialog = null; string[] listcontent = {"wedding", "anniversary", "naming ceremony/baptism", "thread ceremony", "engagement", "birthday", "friends , family meet", "funeral", "movie", "play"}; switch(id) { case custom_dialog_id: dialog = new dialog(planeventactivity.this); dialog.requestwindowfeature(window.feature_no_title); dialog.setcontentview(r.layout.choose_event_dialog); dialog.setcancelable(true); dialog.setcanceledontouchoutside(true); dialog.setoncancellistener(new dialoginterface.oncancellistener(){ @override public void oncancel(dialoginterface dialog) { // todo auto-generated method stub }}); dialog.setondismisslistener(new dialoginterface.ondismisslistener(){ @override public void ondismiss(dialoginterface dialog) { // todo auto-generated method stub }}); //prepare listview in dialog dialog_listview = (listview)dialog.findviewbyid(r.id.dialoglist); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, listcontent); dialog_listview.setadapter(adapter); dialog_listview.setonitemclicklistener(new adapterview.onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { chooseeventtext.settext(parent.getitematposition(position).tostring()); dismissdialog(custom_dialog_id); }}); break; } return dialog; }
in dialog want show events asynctask. thank you.
not sure if understand question correctly, execute asynctask
, have create instance of registerasynctask
, call execute()
method on it.
registerasynctask task = new registerasynctask(); task.execute(yourmap); // can pass multiple params execute() method
or, if don't need ahold of instance:
new registerasynctask().execute(yourmap);
Comments
Post a Comment