java - Updating class field from callback -
i'm trying connect api using retrofit. need login user , after set "user"
field contains session key required other api calls. need have available before executing code don't know how check or block code until field set. ideas how it?
public class apiclient { public static final string developerkey = ""; public static final string applicationkey = ""; static final string base_url = "https://secure.techfortesco.com/tescolabsapi"; public apiservice mapiservice; public user user; public apiclient() { restadapter restadapter = new restadapter.builder() .setloglevel( restadapter.loglevel.full ) .setendpoint( base_url ) .build(); if(mapiservice == null) { mapiservice = restadapter.create( apiservice.class ); } } public void login() { mapiservice.login( developerkey, applicationkey, new callback<user>() { @override public void success ( user user, response response ) { //code should update user field } @override public void failure ( retrofiterror error ) { } } ); } public interface apiservice { @get ("/restservice.aspx?command=login&email=&password=") public void login ( @query ("developerkey") string developerkey, @query ("applicationkey") string applicationkey, callback<user> callback ); @get ("/restservice.aspx?command=productsearch") public void search ( @query ("searchtext") string searchtext, @query ("sessionkey") string sessionkey, callback<pojo.searchresult> callback); } }
you can try using callback:
example:
public interface logincallback { void ready(); }
and in activity / fragment
public mainactivity extends activity { public void oncreate() { super.oncreate(); apiclient client = new apiclient(); client.login(new logincallback() { @override public void ready() { //... next request in api. } }); } }
and login method became:
public void login(final logincallback logincallback) { mapiservice.login( developerkey, applicationkey, new callback<user>() { @override public void success ( user user, response response ) { //code should update user field logincallback.ready(); } @override public void failure ( retrofiterror error ) { } } );
}