java – 在同一个Activity中有两个接口实现的情况下,Retrofit.Callback的success()和failure()
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 在同一个Activity中有两个接口实现的情况下,Retrofit.Callback的success()和failure(),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3857字,纯文字阅读大概需要6分钟。
内容图文
![java – 在同一个Activity中有两个接口实现的情况下,Retrofit.Callback的success()和failure()](/upload/InfoBanner/zyjiaocheng/808/9c93c628c49f46a9b2667283af8dfaa0.jpg)
我目前正在编写一个连接到服务器以发出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,
Callback<RegistrationResult> resp);
}
现在,因为我将使用GCM进行推送通知,所以我有另一个接口将特定设备的注册ID发送到服务器,并再次获得响应.该界面如下所示:
public interface SendRegIDInterface {
@FormUrlEncoded
@POST("/api/apiregid.php")
void connect(@Field("reg_id") String reg_id,
Callback<RegIDResult> resp);
}
到现在为止还挺好.当我尝试在同一个类中创建两个接口的实现时出现问题.假设我有一个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 /人/
> 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或使用依赖注入(使用Dagger)对每个模型进行单例引用.
因此,根据您的活动,呼叫将是这样的:
...
ServiceLocator.getRegistrationModel().connect(country, state, email, password);
...
或者,如果您添加了自己的回调:
...
ServiceLocator.getRegistrationModel().connect(country, state, email, password,
new RegistrationCallback(boolean result) {
// do something.
}
);
...
内容总结
以上是互联网集市为您收集整理的java – 在同一个Activity中有两个接口实现的情况下,Retrofit.Callback的success()和failure()全部内容,希望文章能够帮你解决java – 在同一个Activity中有两个接口实现的情况下,Retrofit.Callback的success()和failure()所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。