本文介绍了改造,回调 204 No Content 响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 上,我最初实现了这样的 Retrofit 界面:

@DELETE(USER_API_BASE_URL + "/{id}")public void deleteUser(@Path("id") String id, Callback callback);

成功删除后,服务器返回 204 NO CONTENT.这导致回调触发失败,带有 retrofit.RetrofitError: End of input at character 0 of ,因为它期望 User 对象返回响应.>

然后我像这样重写它,使用 Void 而不是 User:

@DELETE(USER_API_BASE_URL + "/{id}")public void deleteUser(@Path("id") String id, Callback callback);<-- 无效

但是我从回调中得到了同样的错误.解决此问题的正确方法是什么?谢谢.

解决方案

Retrofit 2.x 不再有其他答案中提到的 ResponseCallback.您想使用 Response 类型.

RxJava 声明:

@PUT Observable富();

标准声明:

@PUT Call酒吧();

On Android, I initially implemented a Retrofit interface like this:

@DELETE(USER_API_BASE_URL + "/{id}")
public void deleteUser(@Path("id") String id, Callback<User> callback);

The server returns 204 NO CONTENT upon a successful deletion. This was causing the callback to trigger failure, with retrofit.RetrofitError: End of input at character 0 of, as it was expecting a User object back with the response.

I then rewrote it like this, using Void instead of User:

@DELETE(USER_API_BASE_URL + "/{id}")
public void deleteUser(@Path("id") String id, Callback<Void> callback);  <-- VOID

But I am getting the same error from the callback.What is the proper way to fix this? Thank you.

解决方案

Retrofit 2.x no longer has a ResponseCallback as mentioned in the other answer. You want to use a Response<Void> type.

The RxJava declaration:

@PUT Observable<Response<Void>> foo();

The standard declaration:

@PUT Call<Response<Void>> bar();

这篇关于改造,回调 204 No Content 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:48