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
@AbKDs, I don't know if you already solved it, but I found out after some struggle with retrofit 2 that you need to put "http://api.themoviedb.org" as your baseUrl and add the whole path in @GET. Like so: @GET("/3/discover/movie") And then you add your queries as params for your interface Call.
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).
Use an Interceptor from OkHttp On Retrofit 1.9 you could use RequestInterceptor to intercept a Request but it is already removed on Retrofit 2.0 since the HTTP connection layer has been moved to OkHttp.
As a result, we have to switch to an Interceptor from OkHttp from now on. First you have to add it to build.gradle
compile 'com.squareup.okhttp:okhttp:2.5.0'
and then create a OkHttpClient object with an Interceptor like this:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
// Do anything with response here
return response;
}
});
And then pass the created client into Retrofit's Builder chain.
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://api.themoviedb.org/3/discover/").addConverterFactory(GsonConverterFactory.create()).client(client).build();
Remove '/' from your @GET("/abc") because Retrofit 2.0 comes with new URL resolving concept. Base URL and @Url have not just simply been combined together but have been resolved the same way as what does instead. Please take a look for the examples below for the clarification.
To know more about Retrofit 2.0