我尝试使用Retrofit 2.0来实现库系统。 可以添加书籍,列出所有书籍信息,列出一本书籍信息,删除一本书籍,删除所有书籍,更新一本书籍信息。
我的baseURL最后有一个' / ':
http://www.example.com/webservice/
前三个功能非常有效:
@GET("books")
Call<ArrayList<Book>> listBooks();
@POST("books")
Call<Book> addBook(@Body Book book);
@GET("books/{id}")
Call<Book> getBookInfo(@Path("id") int bookId);
但是,这三个根本不起作用:
@DELETE("books/{id}")
Call<Void> deleteBook(@Path("id") int bookId);
@PUT("books/{id}")
Call<Book> updateBook(@Path("id") int bookId , @Body Book book);
@DELETE("clean")
Call<Void> deleteAll();
例如,这是我的deleteBook功能:
Gson gson = new GsonBuilder()
.setDateFormat(Constant.DATE_FORMAT)
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
LibraryService libraryServiceAPI = retrofit.create(LibraryService.class);
Call<Void> deleteBookCall = libraryServiceAPI.deleteBook(bookId);
deleteBookCall.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.isSuccessful()) {
if (response.code() == 204) { // I get 200 here, not 204
// but this is what I find:
// response-->rawResponse-->request-->method == "GET"
// This should be DELETE, am I right?
}
} else {
Log.d(DELETE_BOOK_ERROR, String.valueOf(response.code()));
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Log.d(DELETE_BOOK_ERROR, RESPONSE_FAILURE);
}
});
当我调试数据时:我找到响应 - &gt; rawResponse - &gt; request - &gt; method ==“GET”,在此示例中应为“DELETE”。在我实现updateBook和deleteAll功能后,我发现它们有同样的问题,这里的方法都等于“GET”而不是“PUT”和“DELETE”。
有人能帮我理解为什么会这样吗?非常感谢。
您将在响应中看到检查请求信息,方法是“GET”,但标签显示method =“DELETE”。
Update2:根据Dexter的建议,我添加了HttpLoggingInterceptor进行调试,
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.interceptors().add(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build()).build();
以下是两段日志:
@GET("books/{id}")
Call<Book> getBookInfo(@Path("id") int bookId);
这是正确的。03-28 00:26:00.842 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: - &GT; GET http://www.example.com/56eb7034cada930009ab0998/books/2 http / 1.1 03-28 00:26:00.842 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: - &gt;结束03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:&lt; - 200 OK http://www.example.com/56eb7034cada930009ab0998/books/2/(191ms)03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: 连接:保持活力03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:服务器: gunicorn / 18.0 03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:日期:星期一,3月28日 2016 06:03:30 GMT 03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:Content-Type: application / json 03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:Content-Length: 153 03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:Via:1.1 vegur 03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: OkHttp-Sent-Millis:1459139160940 03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: OkHttp-Received-Millis:1459139161040 03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:{“author”:“123”, “categories”:“123”,“id”:2,“lastCheckedOut”:null, “lastCheckedOutBy”:null,“publisher”:“123”,“title”:“123”,“url”: “/ books / 2 /”} 03-28 00:26:01.038 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:&lt; - END HTTP (153字节的正文)03-28 00:26:01.038 3171-3171 / com.xiaoyaoworm.prolificlibrary D /书籍信息响应代码: 响应状态代码:200
@DELETE("books/{id}")
Call<Void> deleteBook(@Path("id") int bookId);
哪个不对。03-28 00:26:35.602 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: - &GT;删除http://www.example.com/56eb7034cada930009ab0998/books/2 http / 1.1 03-28 00:26:35.602 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: - &gt; END DELETE 03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:&lt; - 200 OK http://www.example.com/56eb7034cada930009ab0998/books/2/(481ms)03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: 连接:保持活力03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:服务器: gunicorn / 18.0 03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:日期:星期一,3月28日 2016 06:04:05 GMT 03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:Content-Type: application / json 03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:Content-Length: 153 03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:Via:1.1 vegur 03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: OkHttp-Sent-Millis:1459139195900 03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp: OkHttp-Received-Millis:1459139196088 03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:{“author”:“123”, “categories”:“123”,“id”:2,“lastCheckedOut”:null, “lastCheckedOutBy”:null,“publisher”:“123”,“title”:“123”,“url”: “/ books / 2 /”} 03-28 00:26:36.082 3171-3198 / com.xiaoyaoworm.prolificlibrary D / OkHttp:&lt; - END HTTP (153字节的正文)
答案 0 :(得分:2)
我自己找到答案。这是一个愚蠢的错误.....当我调试我的回复消息时,我发现在我的删除和放置方法中,响应 - &gt; rawResponse - &GT; priorResponse:响应{protocol = http / 1.1,code = 301,message = MOVED PERMANENTLY,url = http://www.example.com/56eb7034cada930009ab0998/clean}
在我正确的get函数中,priorResponse为null。我在那里阅读了HTTP basic信息,找到了
如果客户向&#34; / testdir /&#34发出GET请求; (即在 目录).......有趣的是,如果客户向&#34; / testdir&#34;发出GET请求,请注意。 (没有指定目录路径&#34; /&#34;),服务器返回&#34; 301永久移动&#34;新的&#34;位置&#34; of&#34; / testdir /" ;,如下。
添加此&#34; /&#34;在我的DELETE,PUT界面url结束,现在所有功能都正常工作!有趣的是在chrome插件中 - &gt;高级休息客户,我不需要这个&#34; /&#34;最终使其发挥作用。
我的更新界面代码现在为:
@GET("books")
Call<ArrayList<Book>> listBooks();
@POST("books")
Call<Book> addBook(@Body Book book);
@GET("books/{id}/")
Call<Book> getBookInfo(@Path("id") int bookId);
@DELETE("books/{id}/")
Call<Void> deleteBook(@Path("id") int bookId);
@PUT("books/{id}/")
Call<Book> updateBook(@Path("id") int bookId, @Body Book book);
@DELETE("clean/")
Call<Void> deleteAll();