java Retrofit 2.0 如何删除?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36251080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:08:57  来源:igfitidea点击:

Retrofit 2.0 how to delete?

javaandroidretrofitretrofit2http-delete

提问by xiaoyaoworm

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?

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

I tried both:

我都试过:

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

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

I get error java.lang.IllegalArgumentException: Service methods cannot return void. for method LibraryService.deleteBook.

我收到错误java.lang.IllegalArgumentException: Service Methods cannot return void。对于方法 LibraryService.deleteBook。

I also gave a try on this:

我也试了一下:

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

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

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

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?

无论我使用 okhttp3.Response 还是 Retrofit2.Response,我都会收到错误消息:'*.Response' is not a valid response body type。你是说 ResponseBody 吗?

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

有人可以给我一个成功的删除示例吗?我在网上用谷歌搜索,但找不到足够的信息。非常感谢。

回答by Lucas Crawford

Do it this way as you noted last:

按照您上次提到的方式执行此操作:

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

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.

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

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.

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

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

Or, Jake Wharton suggests

或者,Hyman沃顿建议

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).

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

So you have:

所以你有了:

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

Usage:

用法:

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

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

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

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