我目前正在编写一个连接到服务器以发出 POST 请求的应用程序。为此,我为各种网络操作创建了多个 Retrofit 接口(interface)。我有一个执行注册的:我获取用户名、电子邮件等,发出 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,
Callback<RegistrationResult> resp);
}
现在,因为我要使用 GCM 推送通知,所以我有另一个接口(interface)将特定设备的注册 ID 发送到服务器,然后再次获得响应。该界面如下所示:
public interface SendRegIDInterface {
@FormUrlEncoded
@POST("/api/apiregid.php")
void connect(@Field("reg_id") String reg_id,
Callback<RegIDResult> resp);
}
到目前为止,还不错。当我尝试在同一个类中创建两个接口(interface)的实现时,问题就出现了。假设我有一个 Activity,出于某种原因,它应该使用来自两个接口(interface)的实现。所以我有这个:
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 接口(interface)的覆盖方法(失败和成功)对应在哪里?由于我在同一个 Activity 中有两个 Retrofit 接口(interface)的实现,我如何区分返回的内容,例如。成功()方法? Callback 的 success() 方法的参数中包含的是来自 RegistrationInterface 实现的响应还是来自 SendRegIDInterface 的响应?只要我在 Activity 中只有 RegistrationInterface 接口(interface)的实现,一切就很清楚了:success() 方法的参数包含服务器对注册请求的响应。现在我正在使用第二个接口(interface)实现 (SendRegIDInterface),我非常困惑!
还是我完全错了?
最佳答案
我认为你需要更多的分离。如果你想在你的 Activity 中放置回调,业务逻辑会与 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
}
}
}
然后我会使用我自己的 service locator 对每个模型进行单例引用。 ,或使用依赖注入(inject)(使用 Dagger )。 因此,从您的 Activity 来看,调用将是这样的:
...
ServiceLocator.getRegistrationModel().connect(country, state, email, password);
...
或者如果您添加了自己的回调:
...
ServiceLocator.getRegistrationModel().connect(country, state, email, password,
new RegistrationCallback(boolean result) {
// do something.
}
);
...
关于java - Retrofit.Callback 的 success() 和 failure() 在同一个 Activity 中有两个接口(interface)实现的情况下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26702533/