Kristijan Petrinić
Kristijan Petrinić

Reputation: 21

MapBox: Request failed due to a permanent error: Attempt to invoke virtual method 'java.lang.String okhttp3.HttpUrl.host()' on a null object reference

I'm trying to download the map with OfflineManager of MapBox SDK. My style is located in the assets folder and works fine when displaying the map online. But when I try to download the map with the same style, I get that error. Is it maybe a problem that my style JSON file is located in assets folder? Should it put it somewhere else?

 mapView.onCreate(savedInstanceState);
    mapView.setStyleUrl("asset://kompassStyle");

    mapView.getMapAsync(mapboxMap -> {
        startDownload(mapboxMap);
    });

 private void startDownload(MapboxMap mapboxMap) {

    byte[] metadata;
    try {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("region", "Some region");
        String json = jsonObject.toString();
        metadata = json.getBytes(JSON_CHARSET);
    } catch (Exception exception) {
        Log.e("Offline", "Failed to encode metadata: " + exception.getMessage());
        metadata = null;
    }

    if (metadata == null)
        return;


    Timber.e("Style " + mapboxMap.getStyleUrl());

    OfflineManager offlineManager = OfflineManager.getInstance(getContext());
    LatLngBounds latLngBounds = new LatLngBounds.Builder()
            .include(new LatLng(45.8150, 15.9819)) // Northeast
            .include(new LatLng(41.9028, 12.4964)) // Southwest
            .build();

    OfflineTilePyramidRegionDefinition definition = new OfflineTilePyramidRegionDefinition(
            mapboxMap.getStyleUrl(),
            latLngBounds,
            5,
            10,
            getContext().getResources().getDisplayMetrics().density);


    offlineManager.createOfflineRegion(definition, metadata, new OfflineManager.CreateOfflineRegionCallback() {
        @Override
        public void onCreate(OfflineRegion offlineRegion) {
            offlineRegion.setDownloadState(OfflineRegion.STATE_ACTIVE);

            offlineRegion.setObserver(new OfflineRegion.OfflineRegionObserver() {
                @Override
                public void onStatusChanged(OfflineRegionStatus status) {
                    double percentage = status.getRequiredResourceCount() >= 0
                            ? (100.0 * status.getCompletedResourceCount() / status.getRequiredResourceCount()) :
                            0.0;

                    if (status.isComplete()) {
                        // Download complete
                        Log.d("offline", "Region downloaded successfully.");
                    } else if (status.isRequiredResourceCountPrecise()) {
                        Log.d("offline", " " + percentage);
                    }
                }

                @Override
                public void onError(OfflineRegionError error) {
                    Timber.e(error.getMessage());
                    Timber.e(error.getReason());
                }

                @Override
                public void mapboxTileCountLimitExceeded(long limit) {
                    Timber.e("LimitOffline " + limit);
                }
            });

        }

        @Override
        public void onError(String error) {
            Timber.e(error);
        }
    });
}

Here is my kompassStyle

    {
      "version": 8,
      "name": "Raster Tiles",
      "sources": {
        "yourTileLayer": {
          "type": "raster",
          "tiles": [
        "http://xxxxxx/{z}/{x}/{y}.jpg?key=something",
        "http://yyyyyy/{z}/{x}/{y}.jpg?key=something",
        "http://ssssss/{z}/{x}/{y}.jpg?key=something",
        "http://nnnnnn/{z}/{x}/{y}.jpg?key=something"
      ],
      "tileSize": 256
    }
  },
  "layers": [
    {
      "id": "background",
      "type": "background",
      "paint": {
        "background-color": "#41afa5"
      }
    },
    {
    "id": "yourTileLayer",
    "type": "raster",
    "source": "yourTileLayer"
  }]
}

error log: W/HTTPRequest: Request failed due to a permanent error: Attempt to invoke virtual method 'java.lang.String okhttp3.HttpUrl.host()' on a null object reference

Upvotes: 1

Views: 2340

Answers (1)

Kristijan Petrinić
Kristijan Petrinić

Reputation: 21

After a long research finally found the solution. Found the solution here: https://github.com/mapbox/mapbox-gl-native/issues/5271

Upvotes: 1

Related Questions