Source:
public void loadLocations(final String url) { loginView.showLocationLoadingAnimation(); if (NetworkUtils.hasNetwork()) { String locationEndPoint = url + ApplicationConstants.API.REST_ENDPOINT + "location"; RestApi restApi = RestServiceBuilder.createService(RestApi.class); Call<Results<Location>> call = restApi.getLocations(locationEndPoint, "Login Location", "full"); call.enqueue(new Callback<Results<Location>>() { @Override public void onResponse(Call<Results<Location>> call, Response<Results<Location>> response) { if (response.isSuccessful()) { RestServiceBuilder.changeBaseUrl(url.trim()); mOpenMRS.setServerUrl(url); loginView.initLoginForm(response.body().getResults(), url); loginView.startFormListService(); loginView.setLocationErrorOccurred(false); } else { loginView.showInvalidURLSnackbar("Failed to fetch server's locations"); loginView.setLocationErrorOccurred(true); loginView.initLoginForm(new ArrayList<Location>(), url); } loginView.hideUrlLoadingAnimation(); } @Override public void onFailure(Call<Results<Location>> call, Throwable t) { loginView.hideUrlLoadingAnimation(); loginView.showInvalidURLSnackbar(t.getMessage()); loginView.initLoginForm(new ArrayList<Location>(), url); loginView.setLocationErrorOccurred(true); } }); } else { LocationDAO.getLocations() .observeOn(AndroidSchedulers.mainThread()) .subscribe(locations -> { if (locations.size() > 0) { loginView.initLoginForm(locations, url); loginView.setLocationErrorOccurred(false); } else { loginView.showToast("Network not available.", ToastUtil.ToastType.ERROR); loginView.setLocationErrorOccurred(true); } loginView.hideLoadingAnimation(); }); } }

Target:
public void loadLocations(final String url) { loginView.showLocationLoadingAnimation(); if (NetworkUtils.hasNetwork()) { String locationEndPoint = url + ApplicationConstants.API.REST_ENDPOINT + "location"; RestApi restApi = RestServiceBuilder.createService(RestApi.class); Call<Results<Location>> call = restApi.getLocations(locationEndPoint, "Login Location", "full"); call.enqueue(new Callback<Results<Location>>() { @Override public void onResponse(Call<Results<Location>> call, Response<Results<Location>> response) { if (response.isSuccessful()) { RestServiceBuilder.changeBaseUrl(url.trim()); mOpenMRS.setServerUrl(url); loginView.initLoginForm(response.body().getResults(), url); loginView.startFormListService(); loginView.setLocationErrorOccurred(false); } else { loginView.showInvalidURLSnackbar("Failed to fetch server's locations"); loginView.setLocationErrorOccurred(true); loginView.initLoginForm(new ArrayList<Location>(), url); } loginView.hideUrlLoadingAnimation(); } @Override public void onFailure(Call<Results<Location>> call, Throwable t) { loginView.hideUrlLoadingAnimation(); loginView.showInvalidURLSnackbar(t.getMessage()); loginView.initLoginForm(new ArrayList<Location>(), url); loginView.setLocationErrorOccurred(true); } }); } else { addSubscription(locationDAO.getLocations() .observeOn(AndroidSchedulers.mainThread()) .subscribe(locations -> { if (locations.size() > 0) { loginView.initLoginForm(locations, url); loginView.setLocationErrorOccurred(false); } else { loginView.showToast("Network not available.", ToastUtil.ToastType.ERROR); loginView.setLocationErrorOccurred(true); } loginView.hideLoadingAnimation(); })); } }