Android Asynchronous Http Client (loopj) and the Persistent Cookie Store -
i new android development. had few questions on best way make libraries mentioned above work optimally.
currently, have 3 activities in app. mainactivity, loginactivity , homeactivity. app launches mainactivity supposed check if person logged in. if person logged in, redirect home, else redirect login.
as mentioned in documentation, created restclient class. can make request in loginactivity , response. code login.
public void login() { requestparams params = new requestparams(); params.put(auth_parameter_email, memail); params.put(auth_parameter_password, mpassword); restclient.setcookiestore(new persistentcookiestore(this)); restclient.post(auth_url, params, new jsonhttpresponsehandler() { @override public void onfinish() { showprogress(false); } @override public void onsuccess(jsonobject response) { string response_status = null; try { response_status = response.getstring(auth_response_status); } catch (jsonexception e) { toast.maketext(loginactivity.this, "error: " + e.tostring(), toast.length_long).show(); log.e(tag, e.tostring()); } if (response_status.equals(auth_success_status)) { finish(); } else { mpasswordview .seterror(getstring(r.string.error_incorrect_password)); mpasswordview.requestfocus(); } } @override public void onfailure(throwable e, string content) { toast.maketext(loginactivity.this, "error: " + e.tostring(), toast.length_long).show(); log.e(tag, e.tostring()); } }); } questions
- this going create new cookie store each time request made. should put created once? should put in oncreate of mainactivity , assign global variable? best practice?
- in mainactivity, how can check session cookie sent server? know in shared preferences, how can it? documentation doesnt variable stored under in sharedpreferences.
- when need logout someone, delete shared preferences or erase cookie store or both? automatically kept in sync?
- when app restarts, how initialise cookie store saved data in sharedpreferences?
- if know of open-sourced code implements this, ill happy @ , answer these questions myself. provide link!
i realize level of understanding here low please bare me! :)
so here have done until now. works, not sure if best practice.
1) cookie store initialises sharedpreference. create new 1 each time need it. sure use same context each time. using getapplicationcontext()
2) , 4) cookie store handels you. create new 1 same context 1 created earlier. long consistent, cookies initialise properly.
3) cookie store keeps shared preferences , local attributes in sync call (new persistentcookiestore(getapplicationcontext())).clear();
my code
restclient.java
public static void setcookiestore(persistentcookiestore cookiestore) { client.setcookiestore(cookiestore); } loginactivity.java
restclient.setcookiestore(new persistentcookiestore(getapplicationcontext())); mainactivity.java
private void loginrouter() { persistentcookiestore mcookiestore = new persistentcookiestore( getapplicationcontext()); list<cookie> cookies = mcookiestore.getcookies(); (cookie c : cookies) { if (c.getname().equals("session")) { startactivity(new intent(this, homeactivity.class)); finish(); } } launchsplashpage(); }
Comments
Post a Comment