Android Retrofit return status 500 internal server error

前端 未结 3 739
臣服心动
臣服心动 2021-01-19 04:05

I am using Retrofit like this to get all the books and delete all the books.

 @GET(\"/books\")
    BookListResponse getAllBooks();
    @DELETE(\"/clean\")
           


        
相关标签:
3条回答
  • 2021-01-19 04:28

    Update
    In Retrofit 2.1 the request builder has changed so you have to specify header in the retrofit interface methods

    @Headers("Accept: application/json") @GET("products") Call<Products> getAllProducts();

    0 讨论(0)
  • 2021-01-19 04:35

    This is just a matter of semantics: when you say to your server "Please delete book", but you don't say which book you want to delete, the server don't know what to do (this happens when you send a DELETE to /books). Hence the error 500. But when you say "Please delete book with id 1", now the server knows what to do (this happens when you send a DELETE to /books/1).

    It is ok to work that way if you setup your server like this, but I've never seen any REST service to delete ALL records of a particular model. Again, if you coded your server this way it is ok, just make sure the request is reaching the server the way you want.

    After all, if you got a error 500, check your server. The problem isn't in the Android side, definitely.

    0 讨论(0)
  • 2021-01-19 04:41

    Just add the header accept in the request as follow:

    Request request = new Request.Builder()
                    .url(urlString)
                    .header("accept", "application/json")
                    .build();
    
    0 讨论(0)
提交回复
热议问题