public static boolean testApiKey(String key) throws InterruptedException {
log.info("Using API key {}", key);
ArcGISRuntimeEnvironment.setApiKey(key);
final int MAX_WAIT_TIME = 2000; // millisecs
try {
URL urlObj = new URL(sUrl);
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(MAX_WAIT_TIME);
con.connect();
int code = con.getResponseCode();
if (code != 200) {
log.error("Unable to connect to ESRI basemaps API site \"{}\"", sUrl);
return false;
}
} catch (Exception e) {
log.error("Exception ecountered while trying to connect to the ESRI basemaps API site \"{}\": {}", sUrl, e.getMessage());
return false;
}
final MapView mapView = new MapView();
final int INTERVAL = 100;
int waitTime = 0;
final ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY);
final HashMap<String, BaseMapLoadStatus> loadStatusMap = new HashMap<>();
final BaseMapLoadStatus ls = new BaseMapLoadStatus(LoadStatus.NOT_LOADED);
loadStatusMap.put(ls.getName(), ls);
map.addLoadStatusChangedListener(e -> {
BaseMapLoadStatus ls1 = new BaseMapLoadStatus(e.getNewLoadStatus());
loadStatusMap.put(ls1.getName(), ls1);
});
mapView.setMap(map);
log.debug("Waiting for map to load");
while(waitTime < MAX_WAIT_TIME && !loadStatusMap.containsKey(LoadStatus.LOADED.name())) {
Thread.sleep(100);
waitTime += INTERVAL;
log.debug("Waiting {} msecs", waitTime);
}
if (waitTime >= MAX_WAIT_TIME) {
log.error("Loading of base map timed out. Waited {} msecs for map to load", waitTime);
return false;
}
List<BaseMapLoadStatus> sorted = ls.sortByTimestamp(loadStatusMap);
log.debug("Load status change of states:");
for (BaseMapLoadStatus bmls : sorted) {
log.debug("{} {}", bmls.getTimestamp(), bmls.getName());
}
return loadStatusMap.containsKey(LoadStatus.LOADED.name());
}