synchronized public void requestRefreshRepository() { if (checkingInProgress) return; Gdx.app.log(TAG, "Requesting new data from remote server."); checkingInProgress = true; eventDispatcher.postEvent(new ModuleRepositoryRefreshEvent(ModuleRepositoryRefreshEvent.Action.REFRESH_STARTED)); Gdx.net.sendHttpRequest(new HttpRequestBuilder().newRequest() .method(Net.HttpMethods.GET) .url(getRelativeUrl("modules.json")) .timeout(10000) .header(HttpRequestHeader.CacheControl, "no-cache") .build(), new Net.HttpResponseListener() { @Override public void handleHttpResponse(Net.HttpResponse httpResponse) { try { String result = httpResponse.getResultAsString(); // Cache result into local file FileUtils.saveTextToFile(repoCacheFile, result); // Update in-memory data Array<RepositoryModuleData> newArray = json.fromJson(Array.class, RepositoryModuleData.class, result); repositoryModules.clear(); for (RepositoryModuleData moduleData : newArray) { repositoryModules.put(moduleData.name, moduleData); } prefsCommon.putLong(PREF_KEY_LAST_CHECK, new Date().getTime()).flush(); Gdx.app.log(TAG, "Data was loaded from remote server."); checkingInProgress = false; eventDispatcher.postEvent(new ModuleRepositoryRefreshEvent(ModuleRepositoryRefreshEvent.Action.REFRESH_FINISHED)); eventDispatcher.postEvent(new ModuleRepositoryRefreshEvent(ModuleRepositoryRefreshEvent.Action.FINISHED_SUCCESS)); } catch (Exception e) { checkingInProgress = false; eventDispatcher.postEvent(new ModuleRepositoryRefreshEvent(ModuleRepositoryRefreshEvent.Action.REFRESH_FINISHED)); eventDispatcher.postEvent(new ModuleRepositoryRefreshEvent(ModuleRepositoryRefreshEvent.Action.FINISHED_ERROR)); } } @Override public void failed(Throwable t) { checkingInProgress = false; eventDispatcher.postEvent(new ModuleRepositoryRefreshEvent(ModuleRepositoryRefreshEvent.Action.REFRESH_FINISHED)); eventDispatcher.postEvent(new ModuleRepositoryRefreshEvent(ModuleRepositoryRefreshEvent.Action.FINISHED_ERROR)); } @Override public void cancelled() { checkingInProgress = false; eventDispatcher.postEvent(new ModuleRepositoryRefreshEvent(ModuleRepositoryRefreshEvent.Action.REFRESH_FINISHED)); eventDispatcher.postEvent(new ModuleRepositoryRefreshEvent(ModuleRepositoryRefreshEvent.Action.FINISHED_ERROR)); } }); }