Android。在客户端-服务器应用程序中,当一条消息从客户端发布时,它会被复制
Android. In a client-server application, when a message is posted from a client, it is duplicated
我正在为 Android 编写客户端-服务器应用程序。我有一个问题,我无法追踪它。重点是:
我有一个 API 方法。我使用 Retrofit 2 从设备发送一个 POST 请求(但是,由于我在服务器上使用 REQUEST 数组,该方法是无关紧要的,我尝试使用相同的 GET)。该方法假设 post 在论坛上留言。结果,它被放置了两次,时间恰好为一秒。我从日志中获取请求并从浏览器发送它,用户代理显示在设备上,一切正常 - 只发布了一个 post。
改造方法:
@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 只被调用一次。在日志中查看 - 请求以相同的方式发送一次。可能是什么问题呢?
求助
也许有一天它会对某人有所帮助。我在开发此应用程序的最开始时编写代码以在日志中查看服务器的响应:
// 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);
});
从代码可以看出,从这里又飞来了一个请求,我一直找不到。是我不细心,谢谢大家想过我的问题
我正在为 Android 编写客户端-服务器应用程序。我有一个问题,我无法追踪它。重点是: 我有一个 API 方法。我使用 Retrofit 2 从设备发送一个 POST 请求(但是,由于我在服务器上使用 REQUEST 数组,该方法是无关紧要的,我尝试使用相同的 GET)。该方法假设 post 在论坛上留言。结果,它被放置了两次,时间恰好为一秒。我从日志中获取请求并从浏览器发送它,用户代理显示在设备上,一切正常 - 只发布了一个 post。
改造方法:
@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 只被调用一次。在日志中查看 - 请求以相同的方式发送一次。可能是什么问题呢? 求助
也许有一天它会对某人有所帮助。我在开发此应用程序的最开始时编写代码以在日志中查看服务器的响应:
// 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);
});
从代码可以看出,从这里又飞来了一个请求,我一直找不到。是我不细心,谢谢大家想过我的问题