0

In my Android app, I have the following logout function:

suspend fun logout(token: String): NetworkResult<Unit> {
      return try{
          val logoutResponse = service.logout("Token $token")
          NetworkResult.Success(logoutResponse)
      }catch(e:Throwable){
          NetworkResult.Error(null)        // <-- after a successful logout, we proceeed with this line WHY ? 
      }
}

The API interface containing the endpoints contains this:

@POST("logout")
suspend fun logout(@Header("Authorization") token: String)

On the server side, I use django-rest-knox as 3rd party library for authentication. The logout endpoint of that library has a built-in LogoutView which returns "204 no content" response after a successful logout. The documentation recommends to not alter the LogoutView since modifications would lead to unpredictable results. But although the logout request was successful on the server-side, Retrofit considers the "204 no content" response as an exception. As I mentioned in the code snippet above, the catch clause is executed.

Now to my question(s): When the request is successful, why does Retrofit throws an exception? How to deal with "204 no content" responses at all ?

abdullah celik
  • 511
  • 1
  • 6
  • 17
  • https://stackoverflow.com/questions/27940277/retrofit-callback-for-204-no-content-response – P.Juni Jun 01 '21 at 10:48
  • @P.Juni I know this. does not worked for me – abdullah celik Jun 01 '21 at 10:52
  • What exception are you getting? – laalto Jun 01 '21 at 11:15
  • @laalto: As mentioned above: What I get is a "204 - no content" message. Although this is not an exception, Retrofit considers this as an exception. – abdullah celik Jun 01 '21 at 14:11
  • The execution goes to the catch block so there is an exception that you are ignoring now - at least log it to learn more about the problem. I have an idea what might be wrong but would want to see the exception first to see if that is the problem. – laalto Jun 01 '21 at 14:21
  • @laalto: after printing the stacktrace I got this: System.err: kotlin.KotlinNullPointerException: Response from celik.abdullah.authentication.network.AuthApi.logout was null but response body type was declared as non-null – abdullah celik Jun 01 '21 at 14:46
  • Not the issue I had in mind but do change the implicit `Unit` return type to explicit `Response` in your retrofit interface as mentioned in the linked question. – laalto Jun 02 '21 at 10:07

0 Answers0