Using Call Enqueue function in Retrofit

前端 未结 3 1699
说谎
说谎 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:30

    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

提交回复
热议问题