【发布时间】:2017-10-23 12:21:15
【问题描述】:
我正在为 Android 编写一个客户端-服务器应用程序。我有一个问题,我无法跟踪它。重点是: 我有一个 API 方法。我使用 Retrofit 2 从设备发送一个 POST 请求(但是,由于我在服务器上使用 REQUEST 数组,因此该方法无关紧要,并且我尝试使用相同的 GET)。该方法假定在论坛上发布消息。结果,它被放置了两次,时间一致到一秒。我从日志中获取请求并从浏览器发送它,显示在设备上的用户代理,一切正常 - 只发布了一篇文章。
改造方法:
@FormUrlEncoded
@POST("api/index.php?m=topic&action=send")
Observable<ApiResponse> sendPost(@Field("id_topic") long id,
@Field("subject") String subject,
@Field("message") String text);
方法调用:
public void sendPost(String subj, String text) {
Subscription subscription = model.sendPost(args.getLong("topic_id"), subj,
text)
.subscribe(new Observer<ApiResponse>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
try {
Log.e("Post wrong", "Error while posted message: " + e.getMessage());
activityCallback.makeToast("Message not published, an error occurred");
} catch (NullPointerException e1) {
Log.e("Error:", "getMessaage() or activityCallback is null");
}
}
@Override
public void onNext(ApiResponse apiResponse) {
try{
activityCallback.makeToast("Message successfully published");
} catch (NullPointerException e) {
Log.e("Error:", "activityCallback is null");
}
reload();
}
});
addSubscription(subscription);
}
点击:
@OnClick(R.id.sendButton)
public void send() {
if (sendText.getText() == null
|| sendText.getText().toString().trim().isEmpty()){
makeToast("Введите текст публикации");
return;
}
presenter.sendPost("Re: Тестовая тема", sendText.getText().toString());
}
onClick 只被调用一次。在查看的日志中 - 请求以相同的方式发送一次。可能是什么问题呢? 请帮忙
【问题讨论】:
-
你能把
addSubscription(subscription)方法的代码也贴出来吗? -
我的问题解决了,谢谢!
标签: android rest rx-java retrofit2 rx-android