Retrofit 2.0 如何删除? [英] Retrofit 2.0 how to delete?

查看:57
本文介绍了Retrofit 2.0 如何删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Retrofit 2.0 并且我正在我的 Android 应用程序中实现删除功能,但是我无法成功,有人可以给我建议吗?

I am using retrofit 2.0 and I am implementing a delete feature in my Android app, however, I cannot make it successfully, can someone give me a suggestion?

我都试过了:

@DELETE("books/{id}") void deleteBook(@Path("id") int itemId);

@DELETE("books/{id}") void deleteBook(@Path("id") int bookId, Callback<Response> callback);

我收到错误 java.lang.IllegalArgumentException:服务方法不能返回 void.用于方法 LibraryService.deleteBook.

我也试了一下:

Response deleteBook(@Path("id") int bookId);

调用<响应>deleteBook(@Path("id") int bookId);

无论我使用 okhttp3.Response 还是 Retrofit2.Response,我都会收到错误消息:'*.Response' 不是有效的响应正文类型.您是说 ResponseBody 吗?

no matter I use okhttp3.Response or retrofit2.Response, I will get the error: '*.Response' is not a valid response body type. Did you mean ResponseBody?

谁能给我一个成功删除的例子?我在网上用谷歌搜索,但找不到足够的信息.非常感谢.

Can someone give me a successful delete example? I googled online but cannot find enough information. Thanks a lot.

推荐答案

按照你上次提到的方法去做:

Do it this way as you noted last:

Call<ResponseBody> deleteBook(@Path("id") int bookId);

确保通过 AsyncTask 或其他一些线程机制调用 UI 线程.不确定您之前是否使用过 RxJava + Retrofit 2,但它很不错.

Make sure you make the call off the UI-thread via AsyncTask or some other threading mechanism. Not sure if you've used RxJava + Retrofit 2 before, but it is nice.

ResponseBody 对象将返回调用的结果.这是我用于某些不返回实体对象的 REST API 请求的方法,我所关心的只是查看响应代码.

The ResponseBody object will return the results from the call. It is what I use for some REST API requests that don't return an entity object, and all I care about is looking at the response code.

Call<ResponseBody> deleteRequest = mService.deleteBook(123);
deleteRequest.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        // use response.code, response.headers, etc.
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        // handle failure
    }
});

或者,杰克沃顿建议

使用 Void 不仅具有更好的语义,而且在空情况下(稍微)更有效,在非空情况下(当您不关心正文时)效率更高.

Use Void which not only has better semantics but is (slightly) more efficient in the empty case and vastly more efficient in a non-empty case (when you just don't care about body).

所以你有:

Call<Void> deleteBook(@Path("id") int bookId);

用法:

deleteRequest.enqueue(new Callback<Void>() {
    @Override
    public void onResponse(Call<Void> call, Response<Void> response) {
        // use response.code, response.headers, etc.
    }

    @Override
    public void onFailure(Call<Void> call, Throwable t) {
        // handle failure
    }
});

如果您只关心响应代码而不关心响应主体,那就更好了

This is better if all you care about is the response code and no body to the response

编辑 2:省略了正确的回调定义.固定:)

EDIT 2: Left out the proper Callback definition. Fixed :)

这篇关于Retrofit 2.0 如何删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆