From 15f4e8647f668fcb628aa0edbf35b980ca16edb7 Mon Sep 17 00:00:00 2001 From: leopoldsedev Date: Sun, 9 Feb 2020 14:49:26 +0100 Subject: [PATCH] Fix freeze when login URL has trailing whitespace. The problem is that HttpUrl.parse parses URLs with trailing whitespace without problems, but during the URL parsing in ClientFactory an exception is thrown in that case. Other errors when instantiating the API client are now handled as well. --- .../github/gotify/login/LoginActivity.java | 22 ++++++++++++++----- app/src/main/res/values/strings.xml | 3 ++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/github/gotify/login/LoginActivity.java b/app/src/main/java/com/github/gotify/login/LoginActivity.java index 708dd20..235d96f 100644 --- a/app/src/main/java/com/github/gotify/login/LoginActivity.java +++ b/app/src/main/java/com/github/gotify/login/LoginActivity.java @@ -115,11 +115,23 @@ public class LoginActivity extends AppCompatActivity { checkUrlProgress.setVisibility(View.VISIBLE); checkUrlButton.setVisibility(View.GONE); - final String fixedUrl = url.endsWith("/") ? url.substring(0, url.length() - 1) : url; + final String trimmedUrl = url.trim(); + final String fixedUrl = + trimmedUrl.endsWith("/") + ? trimmedUrl.substring(0, trimmedUrl.length() - 1) + : trimmedUrl; - ClientFactory.versionApi(fixedUrl, tempSSLSettings()) - .getVersion() - .enqueue(callInUI(this, onValidUrl(fixedUrl), onInvalidUrl(fixedUrl))); + try { + ClientFactory.versionApi(fixedUrl, tempSSLSettings()) + .getVersion() + .enqueue(callInUI(this, onValidUrl(fixedUrl), onInvalidUrl(fixedUrl))); + } catch (Exception e) { + checkUrlProgress.setVisibility(View.GONE); + checkUrlButton.setVisibility(View.VISIBLE); + String errorMsg = + getString(R.string.version_failed, fixedUrl + "/version", e.getMessage()); + Utils.showSnackBar(LoginActivity.this, errorMsg); + } } public void showHttpWarning() { @@ -301,7 +313,7 @@ public class LoginActivity extends AppCompatActivity { } private String versionError(String url, ApiException exception) { - return getString(R.string.version_failed, url + "/version", exception.code()); + return getString(R.string.version_failed_status_code, url + "/version", exception.code()); } private SSLSettings tempSSLSettings() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bb40d50..d6d881c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,7 +4,8 @@ Close navigation drawer Navigation header Found Gotify v%s - Request to \'%s\' failed with status code %d + Request to \'%s\' failed with status code %d + Request to \'%s\' failed. %s. There is no user with this username and password Client Name Chose a name for your session