When an API key is bad resulting in an application failing to download a base map, the runtime outputs this message to stdout, "com.esri.arcgisruntime.internal.io.handler.request.BaseRequest getInvalidApiKeyException?" How to we catch this exception so that we can inform the user that the API key is bad? Right now, it just produces an error with a INFO log statement which a user will not see. We want to catch it and display an alert box in the GUI when the key is bad.
Solved! Go to Solution.
You can identify this state in code by adding load listener to the map which is trying to use your bad API key.
I've done this very crudely in the code below:
// authentication with an API key or named user is required to access basemaps and other location services
ArcGISRuntimeEnvironment.setApiKey("bad API key");
// create a map with the standard imagery basemap style
ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY);
map.addDoneLoadingListener(()-> {
System.out.println("load status" + map.getLoadStatus());
if (map.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
System.out.println("error " + map.getLoadError().getCause());
}
});
// create a map view and set the map to it
mapView = new MapView();
mapView.setMap(map);
We don't throw a specific error for this condition (IOException), but the cause comes up as you spotted in the console window.
Does this help?
You can identify this state in code by adding load listener to the map which is trying to use your bad API key.
I've done this very crudely in the code below:
// authentication with an API key or named user is required to access basemaps and other location services
ArcGISRuntimeEnvironment.setApiKey("bad API key");
// create a map with the standard imagery basemap style
ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY);
map.addDoneLoadingListener(()-> {
System.out.println("load status" + map.getLoadStatus());
if (map.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
System.out.println("error " + map.getLoadError().getCause());
}
});
// create a map view and set the map to it
mapView = new MapView();
mapView.setMap(map);
We don't throw a specific error for this condition (IOException), but the cause comes up as you spotted in the console window.
Does this help?
Hi Mark,
Thank you very much for the solution. Trying to load a standard basemap as a test of the validity of the API key is a good approach. I implemented it slightly differently. We're using JavaFX and the user is interacting with the UI thread, but listener is attached to a background thread. We need feedback on the UI thread and need the test to be blocking. Also, a bad Internet connection causes a basemap to fail to load in the same way a bad API key does and we have to be able to distinguish between the two problems. Anyway, using a variation on your idea, I came up with the code below. The method "testApiKey(String key)" runs in the foreground, is blocking (with a timeout) and returns true if the key is good and false if it is bad. The "BaseMapLoadStatus" is a helper class for debugging.