How to add query parameters to a HTTP GET request by OkHttp?

前端 未结 7 1206
时光取名叫无心
时光取名叫无心 2020-12-06 04:11

I am using the latest okhttp version: okhttp-2.3.0.jar

How to add query parameters to GET request in okhttp in java ?

I found a related ques

7条回答
  •  失恋的感觉
    2020-12-06 04:38

    Here's my interceptor

        private static class AuthInterceptor implements Interceptor {
    
        private String mApiKey;
    
        public AuthInterceptor(String apiKey) {
            mApiKey = apiKey;
        }
    
        @Override
        public Response intercept(Chain chain) throws IOException {
            HttpUrl url = chain.request().httpUrl()
                    .newBuilder()
                    .addQueryParameter("api_key", mApiKey)
                    .build();
            Request request = chain.request().newBuilder().url(url).build();
            return chain.proceed(request);
        }
    }
    

提交回复
热议问题