使用拦截器在Retrofit调用

时间:2017-08-31 17:47:33

标签: android retrofit2 interceptor

我正在尝试使用Interceptor在使用Retrofit时添加标头。我想我已经以正确的方式创建了我的拦截器,但我不知道该怎么做才能调用它并将它与我的GET Retrofit方法连接起来。

这是我的拦截器:

public class HeaderInterceptor
            implements Interceptor {
        @Override
        public Response intercept(Chain chain)
                throws IOException {
            Request request = chain.request();
            request = request.newBuilder()
                    .addHeader(Constants.VersionHeader.NAME, Constants.VersionHeader.VALUE)
                    .addHeader("Authorization", "Bearer " + token)
                    .addHeader("Origin","MY URL")
                    .build();
            Response response = chain.proceed(request);
            return response;
        }
    }

这是我的界面:

public interface CategoryService {

    @GET("/v3/projects/{projectId}/categories/")
    Call<ArrayList<Category2>> getProjectCategories(@Path("projectId") String projectId);
}

我也有这个客户端,我不知道我是否应该再使用它,因为我正在使用拦截器:

public class CategoryClient {

    public static final String BASE_URL = "MY URL";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

所以我有这个GET方法getProjectCategories,我传递projectID并返回内容。我想知道的是如何使用Interceptor调用该方法并能够从请求中获取结果。

1 个答案:

答案 0 :(得分:0)

我能够通过创建一个名为SendNetworkRequest的方法来解决我的问题,发送projectId作为参数,在这个类中我创建了我的OkHttpClient,我的Interceptor和我的改造构建器来处理我需要的所有东西。

private void SendNetworkRequest(String projectID) {

        OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();

        okhttpBuilder.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {

                Request request = chain.request();

                Request.Builder newRequest = request.newBuilder().header("Authorization", "Bearer " + token);

                return chain.proceed(newRequest.build());
            }
        });

        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("MY URL")
                .client(okhttpBuilder.build())
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit = builder.build();

        CategoryService category = retrofit.create(CategoryService.class);
        Call<ArrayList<Category2>> call = category.getProjectCategories(projectID, token);
        call.enqueue(new Callback<ArrayList<Category2>>() {
            @Override
            public void onResponse(Call<ArrayList<Category2>> call, Response<ArrayList<Category2>> response) {

                listCategories = response.body();
                listCategories.remove(response.body().size() - 1);
                if (response.body().size() > 0){
                    add_category_layout.setVisibility(View.VISIBLE);
                    layout_bar.setVisibility(View.VISIBLE);
                    message_body.setVisibility(View.INVISIBLE);
                    message_title.setVisibility(View.INVISIBLE);
                    edit_image.setVisibility(View.INVISIBLE);
                    adapter2 = new CategoryAdapter2(getApplicationContext(), listCategories);
                    recyclerView.setAdapter(adapter2);
                    recyclerView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(Call<ArrayList<Category2>> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });
    }