android - Returning to calling function when a http request has finished -
i'm trying implement login using asp.net web api android application.
have far functions work, want make login request kind of synchronous instead of asynchronous. i'm using android asynchronous http client on website.
public class apiinterface { public static final string apiurl = "http://******/api/"; private static asynchttpclient client = new asynchttpclient(); public static void get4login(string url, requestparams params, asynchttpresponsehandler responsehandler) { client.get(getabsoluteurl(url), params, responsehandler); } private static string getabsoluteurl(string relativeurl) { return apiurl + relativeurl; } }
and have function in loginactivity:
private boolean dologin(string user, string pass) { boolean result = false; if (user.trim().isempty() || pass.trim().isempty()) { return false; } requestparams params = new requestparams(); params.add("user", user); params.add("pass", pass); apiinterface.get4login("auth", params, new texthttpresponsehandler() { @override public void onfailure(int statuscode, header[] headers, string responsestring, throwable error) { toast.maketext(myapp.getcontext(), "error: " + error.getmessage(), toast.length_long).show(); } @override public void onsuccess(int statuscode, header[] headers, string responsestring) { //***here want set dologin() function result depending on response server;*** toast.maketext(myapp.getcontext(), "lista sesizari incarcata!", toast.length_long).show(); } }); return result; }
is there way this?
on mytexthttpresponsehandler
class should define variable named result , set type boolean,default false,then define method result value,like public boolean getresult(){return this.result;}
can change result
value on onsuccess
, onfailure
method. next dologin
method this
private boolean dologin(string user, string pass) { //boolean result = false; if (user.trim().isempty() || pass.trim().isempty()) { return false; } requestparams params = new requestparams(); params.add("user", user); params.add("pass", pass); mytexthttpresponsehandler mytexthttpresponsehandler = new mytexthttpresponsehandler(this); apiinterface.get4login("auth", params, mytexthttpresponsehandler); return mytexthttpresponsehandler.getresult(); }
Comments
Post a Comment