How to implement a login in Android using Volley? -
i have loginactitvity 2 textfields username , password , login-button. when user presses button, app starts async task. async task implements volleyhandler. checks if login parameters correct , fetches user data (using volley , json). while doing this, progressdialog appears. when async task finished, starts intent mainactivity in onpostexecute method.
now question: idea make volley-requests in async task, or have better solution?
thanks.
you cannot use asynctask. volley care it. can use callback work data , ui. looks this:
public class loginactivity extends singlepaneactivity implements downloadcallback { //... public void sendrequest(){ downloader download = new download(this); downloader.download(username, password); progresbar.show(); } public void requestfinish(){ progersbar.dismis(); //... continue } }
callback:
public interface downloadcallback { void requestfinish(); }
in class downloader
private requestqueue requestqueue; downloadcallback mcallback; public void downloader(downloadcallback callback){ mcallback = callback; requestqueue = volley.newrequestqueue(mcontext); initvolleylisteners(); } private void initvolleylisteners() { msuccesslistener = new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { mcallback.requestfinish(); } }; merrorlistener = new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { mcallback.requestfinish(); } }; public void download(string user, string pass){ jsonobjectrequest jsonobjectrequest = new jsonobjectrequest(request.method.post, url, createjson(user, pass), msuccesslistener , merrorlistener ) { //header send json server @override public map<string, string> getheaders() throws authfailureerror { hashmap<string, string> headers = new hashmap<string, string>(); headers.put("content-type", "application/json; charset=utf-8"); return headers; } }; requestqueue.add(jsonobjectrequest ); }
and 1 point. don't send user name in json. send param in header. use application/x-www-form-urlencoded
, set pass username params in header.
update: work. sorry wrote in hurry. article how callback work
Comments
Post a Comment