java - Intent to HomeActivity after login status success -
this loginactivity.java
this send login details httpasynctask .java
final button button = (button) findviewbyid(r.id.btnlogin); button.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { try { // email edit view value string email = emailet.gettext().tostring(); // password edit view value string password = pwdet.gettext().tostring(); // instantiate http request param object // requestparams params = new requestparams(); // when email edit view , password edit view have values // other null if (utility.isnotnull(email) && utility.isnotnull(password)) { // when email entered valid if (utility.validate(email)) { //call async task jsonobject js = new httpasynctask(getapplicationcontext()).execute(email,password).get(); toast.maketext(getapplicationcontext(), "asynctask started", toast.length_short).show(); } // when email invalid else { toast.maketext(getapplicationcontext(), "please enter valid email", toast.length_long).show(); } } // when of edit view control left blank else { toast.maketext( getapplicationcontext(), "please fill form, don't leave field blank", toast.length_long).show(); } } catch (exception ex) { } } }); } }
httpasynctask.java
public class httpasynctask extends asynctask<string, integer, jsonobject> { private static inputstream stream = null; private static string api; private jsonobject responsejson = null; private context contxt; private activity activity; public httpasynctask(context context) { // api = apiurl; this.contxt = context; } // async task accept string array context array @override protected jsonobject doinbackground(string... params) { string path = null; string response = null; hashmap<string, string> request = null; jsonobject requestjson = null; defaulthttpclient httpclient = null; httppost httppost = null; stringentity requeststring = null; responsehandler<string> responsehandler = null; // username , password log.i("email", params[0]); log.i("password", params[1]); try { path = "http://192.168.xxxxxxx"; new url(path); } catch (malformedurlexception e) { e.printstacktrace(); } try { // set api request request = new hashmap<string, string>(); request.put(new string("email"), params[0]); request.put(new string("password"), params[1]); request.entryset().iterator(); requestjson = new jsonobject(request); httpclient = new defaulthttpclient(); httppost = new httppost(path); requeststring = new stringentity(requestjson.tostring()); httppost.setentity(requeststring); httppost.setheader("content-type", "application/json"); // handles response responsehandler = new basicresponsehandler(); response = httpclient.execute(httppost, responsehandler); responsejson = new jsonobject(response); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } try { responsejson = new jsonobject(response); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } return responsejson; } @override protected void onpostexecute(jsonobject result) { // todo auto-generated method stub super.onpostexecute(result); log.d("myasynctask", "received result: " + result); //here received result: {"status":"400"} }
my problem want know how direct activityhome
when login success, if login error should display error message.
i tried in onpostexecute
method
activity.startactivity(new intent(activity, activityhome.class));
but didn't work, app crashed.
first, have store response in jsonobject
in onpostexecute
method.
protected void onpostexecute(jsonobject result) { // todo auto-generated method stub super.onpostexecute(result); if(responsejson!=null){ try{ jsonobject jobjstatus=new jsonobject(responsejson); string status=jobjstatus.getstring("status"); if(status.equals("success"){ startactivity(new intent(mainactivity.this, secondactivity.class); } }catch(){ } } }
Comments
Post a Comment