You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MapBox Android SDK调用WMS服务时如何添加Authorization请求头

Solution: Adding Authorization Headers for Airbus WMS in MapBox Android SDK

I get it—MapBox’s RasterSource doesn’t let you directly attach custom request headers like the Authorization token Airbus requires, even though you can easily call other WMS services that don’t need this header. Here’s a reliable workaround using a custom OkHttp interceptor to inject the header automatically for your Airbus requests:

Step 1: Create a Custom OkHttp Interceptor

This interceptor will check if the request targets Airbus’s WMS endpoint and add the Authorization header only for those requests (so your other WMS calls remain unaffected):

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;

public class AirbusAuthInterceptor implements Interceptor {
    private final String airbusToken;

    public AirbusAuthInterceptor(String airbusToken) {
        this.airbusToken = airbusToken;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        
        // Only add the header for Airbus WMS requests
        if (originalRequest.url().toString().contains("view.geoapi-airbusds.com")) {
            Request modifiedRequest = originalRequest.newBuilder()
                    .addHeader("Authorization", airbusToken)
                    .build();
            return chain.proceed(modifiedRequest);
        }
        
        // For all other requests, proceed as normal
        return chain.proceed(originalRequest);
    }
}

Step 2: Configure MapBox to Use the Custom OkHttpClient

Tell MapBox’s SDK to use your modified client with the interceptor. Do this early in your app’s initialization (like in your Application class or before initializing the map):

import com.mapbox.mapboxsdk.Mapbox;
import okhttp3.OkHttpClient;

// Initialize MapBox first with your access token
Mapbox.getInstance(context, "YOUR_MAPBOX_ACCESS_TOKEN");

// Build the custom client with the Airbus auth interceptor
OkHttpClient customClient = new OkHttpClient.Builder()
        .addInterceptor(new AirbusAuthInterceptor(Airbus_key))
        .build();

// Set the custom client for MapBox's network operations
Mapbox.getTelemetry().setOkHttpClient(customClient);
Mapbox.getConfiguration().setOkHttpClient(customClient);

Step 3: Set Up Your RasterSource as Usual

Now you can create your Airbus RasterSource without manual header handling—the interceptor will take care of it:

// Fixed the WMS URL to include the required 'srs=' parameter for EPSG:3857
String airBus_source = "https://view.geoapi-airbusds.com/api/v1/map/imagery.wms?version=1.1.1&request=GetMap&service=WMS&WIDTH=256&HEIGHT=256&FORMAT=image/png&srs=EPSG:3857&bbox={bbox-epsg-3857}";

RasterSource airbus_image = new RasterSource(
        "web-map-source",
        new TileSet("tileset", airBus_source),
        256
);
mapboxMap.addSource(airbus_image);

RasterLayer webMapLayer = new RasterLayer("web-map-layer", "web-map-source");
mapboxMap.addLayerBelow(webMapLayer, "aeroway-taxiway");

Key Notes:

  • Targeted Injection: The interceptor only modifies Airbus requests, so your other WMS services (without auth requirements) will work exactly as before.
  • Token Security: Never hardcode your Airbus token directly in the code. Use secure storage like Android’s EncryptedSharedPreferences to store and retrieve it.
  • URL Fix: I added srs= before EPSG:3857 in your WMS URL—this is a standard required parameter that was missing, which might have caused request failures.

内容的提问来源于stack exchange,提问作者sizwe nduduzow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:13:57