【问题标题】:Android. In a client-server application, when a message is posted from a client, it is duplicated安卓。在客户端-服务器应用程序中,当从客户端发布消息时,它会被复制
【发布时间】: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


【解决方案1】:

也许有一天它会帮助某人。我在开发这个应用程序的一开始就写了代码,以便在日志中查看服务器的响应:

// Trace serve response
    clientBuilder.interceptors().add(chain -> {
        Request request = chain.request();
        Response response = chain.proceed(request);
        Log.i("ServerResponse: ", response.body().string());
        return chain.proceed(request);
    });

从代码中可以看出,从这里又发出了一个请求,我一直找不到。我没有专心,我感谢所有考虑我问题的人。

【讨论】:

    猜你喜欢
    • 2019-02-26
    • 2022-01-08
    • 2014-02-28
    • 1970-01-01
    • 2017-01-12
    • 2013-04-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多