Tuesday, 15 March 2011

java - Retrofit.Callback's success() and failure() in case of two interface implementations in same Activity -



java - Retrofit.Callback's success() and failure() in case of two interface implementations in same Activity -

i writing app connects server create post requests. reason, have created multiple retrofit interfaces various network operations. have 1 performs registration: take username, e-mail etc, create post request , final parameter have callback (registrationresult pojo accepts "success" or "failure" in class variable). interface looks this:

public interface registrationinterface { @formurlencoded @post("/api/apiregistration.php") void connect(@field("country") string country, @field("state") string state, @field("email") string email, @field("pwd") string password, callback<registrationresult> resp); }

now, because i'm going utilize gcm force notifications, have interface sends registration id of particular device server and, again, gets response back. interface looks this:

public interface sendregidinterface { @formurlencoded @post("/api/apiregid.php") void connect(@field("reg_id") string reg_id, callback<regidresult> resp); }

so far, good. problem arises when seek create implementations of both interfaces in same class. let's have activity that, reason, should utilize implementations both interfaces. have this:

public class messageactivity extends activity implements callback { public void oncreate(bundle firebug) { restadapter restadapter1 = new restadapter.builder().setendpoint(endpoint).build(); registrationinterface reginter = restadapter1.create(registrationinterface.class); reginter.connect(// parameters here, messageactivity.this); restadapter restadapter2 = new restadapter.builder().setendpoint(endpoint).build(); sendregidinterface regidinter = restadapter2.create(sendregidinterface.class); regidinter.connect(reg_id, messageactivity.this); } @override public void failure(retrofiterror arg0) { } @override public void success(object arg0, response arg1) { } }

my problem this: overridden methods (failure , success) of retrofit.callback interface correspond? since i'm having 2 implementations of retrofit interfaces in same activity, how can distinguish gets returned in, eg. success() method? response registrationinterface implementation or response sendregidinterface that's contained in callback's success() method's arguments? long had implementation of registrationinterface interface in activity, clear: success() method's arguments contain server's response registration request. i'm using sec interface implementation (sendregidinterface) i'm super-confused!

or going wrong this?

i think need bit more separation. if want place callbacks in activity, business logic mess much ui related things.

when utilize retrofit way (will demonstrate code): first, have registrationclient.java, define endpoints api:

public interface registrationclient { @formurlencoded @post("/api/apiregistration.php") void connect(@field("country") string country, @field("state") string state, @field("email") string email, @field("pwd") string password, callback<registrationresult> resp); }

in case, 1 endpoint, there cases, there more, e.g.:

get /persons/{id} post /persons/ put /persons/{id}

when got client, create model interface. name registrationmodel in case:

public class registrationmodel { private registrationclient _client; public registrationmodel() { restadapter restadapter = new restadapter.builder() ... // create adapter _client = restadapter.create(registrationclient.class); } public void connect(string country, string state, string email, string password) { // can add together additional callback parameter returning info caller. _client.connect(country, state, email, password, new callback<registrationresult> { @override public void failure(retrofiterror error) { // essential things, , callback caller if needed } @override public void success(registrationresult result, response response) { // essential things, , callback caller if needed } } }

then hold singleton reference each of models using own service locator, or using dependency injection (with dagger). activity, phone call this:

... servicelocator.getregistrationmodel().connect(country, state, email, password); ...

or if added own callback:

... servicelocator.getregistrationmodel().connect(country, state, email, password, new registrationcallback(boolean result) { // something. } ); ...

java android interface retrofit

No comments:

Post a Comment