java - Return value from inner class in a method -
i building login system android app. using okhttp connect server , json response.
i have defined class login return data (right true/false response based on whether user exists in database), , written code connect server, shown below:
class userlogin { boolean status; public void setstatus(boolean status) { this.status = status; } public boolean getstatus() { return status; } } public class clientserverinterface { okhttpclient client = new okhttpclient(); boolean login(request request) { final gson gson = new gson(); client.newcall(request).enqueue(new callback() { userlogin login; @override public void onfailure(call call, ioexception e) { } @override public void onresponse(call call, response response) throws ioexception { login = gson.fromjson(response.body().charstream(), userlogin.class); login.setstatus(login.status); } }); // need return boolean response (status) here } }
the code passes request
variable login
method works perfectly. want login
return boolean response can pass other methods in other classes.
however, because userlogin
object defined in callback
can't access in parent method. have made getstatus
method not sure how use status in main login
method.
the code passes request variable login method works perfectly. want login return boolean response can pass other methods in other classes.
you can't. enqueue
executes code in async way. don't know when callback invoked. add callback parameter login method. e.g.
boolean login(request request, final callback callback) {
and either pass enqueue
,
client.newcall(request).enqueue(callback);
or call callback manually. e.g.
@override public void onresponse(call call, response response) throws ioexception { if (callback != null) { callback.onresponse(call, response); } }
in both cases caller of login receive callback on provided object and, accordingly content receives, can decide wha actions undertake
Comments
Post a Comment