我目前正在编写一个连接到服务器以发出POST请求的应用程序.出于这个原因,我为各种网络操作创建了多个Retrofit接口.我有一个执行注册:我接受用户名,电子邮件等,发出POST请求然后作为最后一个参数我有一个回调(RegistrationResult是一个POJO,在类变量中接受"成功"或"失败") .此界面如下所示:
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, Callbackresp); }
现在,因为我将使用GCM进行推送通知,所以我有另一个接口将特定设备的注册ID发送到服务器,并再次获得响应.该界面如下所示:
public interface SendRegIDInterface { @FormUrlEncoded @POST("/api/apiregid.php") void connect(@Field("reg_id") String reg_id, Callbackresp); }
到现在为止还挺好.当我尝试在同一个类中创建两个接口的实现时出现问题.假设我有一个Activity,由于某种原因,应该使用来自两个接口的实现.所以我有这个:
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) { } }
我的问题是:Retrofit.Callback接口的重写方法(失败和成功)在哪里对应?由于我在同一个Activity中有两个Retrofit接口实现,我如何区分返回的内容,例如.success()方法?它是来自RegistrationInterface实现的响应还是来自Sendback的success()方法参数中包含的SendRegIDInterface的响应?只要我在Activity中只有一个RegistrationInterface接口的实现,一切都很清楚:success()方法的参数包含服务器对注册请求的响应.现在我正在使用第二个接口实现(SendRegIDInterface),我非常困惑!
或者我对此完全错了?
我认为你需要更多的分离.如果您想将回调放在您的活动中,那么业务逻辑将会与UI相关的东西混乱太多.
当我使用Retrofit时,我这样做(将使用您的代码演示):首先,我有一个RegistrationClient.java
,我定义了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); }
在这种情况下,它只是一个端点,但会有一些情况,其中会有更多,例如:
GET /persons/{id}
POST /persons/
PUT /persons/{id}
当我得到我的客户端,然后我为界面创建一个模型.我会RegistrationModel
在你的情况下命名:
public class RegistrationModel { private RegistrationClient _client; public RegistrationModel() { RestAdapter restAdapter = new RestAdapter.Builder() ... // create an adapter _client = restAdapter.create(RegistrationClient.class); } public void connect(String country, String state, String email, String password) { // you can add an additional callback parameter for returning info to the caller. _client.connect(country, state, email, password, new Callback<RegistrationResult> { @Override public void failure(RetrofitError error) { // Do the essential things, and do a callback to the caller if needed } @Override public void success(RegistrationResult result, Response response) { // Do the essential things, and do a callback to the caller if needed } } }
然后我会使用我自己的服务定位器或使用依赖注入(使用Dagger)对每个模型进行单例引用.因此,根据您的活动,呼叫将是这样的:
... ServiceLocator.getRegistrationModel().connect(country, state, email, password); ...
或者,如果您添加了自己的回调:
... ServiceLocator.getRegistrationModel().connect(country, state, email, password, new RegistrationCallback(boolean result) { // do something. } ); ...