代码之家  ›  专栏  ›  技术社区  ›  Wo'One

安卓在客户机-服务器应用程序中,当从客户机发布消息时,它是重复的

  •  0
  • Wo'One  · 技术社区  · 8 年前

    改造方法:

    @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只调用一次。在查看的日志中,请求以相同的方式发送一次。有什么问题? 求求你,救命

    1 回复  |  直到 8 年前
        1
  •  0
  •   Wo'One    8 年前

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

    // 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);
        });