【发布时间】:2016-06-28 21:56:54
【问题描述】:
我正在使用 Retrofit2 连接到基于 REST 的 API。问题是它需要 lat/lng 位置,但仅适用于大约 50% 的端点。我想知道如何根据一些布尔值将查询参数动态添加到 OkHttpClient 中。我现在的代码:
public static APIEndpointInterface getHttpClient(Boolean addLatLng){
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
LatLng location = App.getLocationRepository().getCurrentLocation();
String lat = String.valueOf(location.latitude);
String lng = String.valueOf(location.longitude);
Request request = chain.request();
HttpUrl url = request.url().newBuilder()
.addQueryParameter("version", API_VERSION)
.addQueryParameter("app_version_code", String.valueOf(BuildConfig.VERSION_CODE))
//ADD ONLY IF BOOLEAN PASSED TO METHOD IS TRUE
.addQueryParameter("lat", lat)
.addQueryParameter("lng", lng)
.port(PORT_HTTPS)
.build();
......
除了将 lat/lng 添加到每隔一个接口方法之外,还有其他选择吗?
【问题讨论】: