Using Call Enqueue function in Retrofit

前端 未结 3 1717
说谎
说谎 2021-02-02 12:49

I am using Retrofit-2.0.0 for my app. Now every tutorial on Retrofit I found on web is based on earlier Retrofit and there was no Call

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 13:25

    With Retrofit 2, onResponse is called even if there is a failure. You have checked whether response is not successful by using !response.isSuccess(). But you just logged it - if it's true (i.e. not successful). Instead, you could log response.errorBody().string() to see if api server specifies the error, and you should call something like return to exit onResponse callback, as response.body() couldn't be casted to MovieResponse, hence the null exception.

    By the way, your code is correct, but if you just start with Retrofit, it would be simpler to use 1.9 version as 2.0 is still a beta version (very stable though, but lack of tutorials).

提交回复
热议问题