diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-10-06 14:07:48 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-10-06 14:07:48 -0700 |
commit | ba9847a0df2a6ca7c1bb570ae8d808f2623687e7 (patch) | |
tree | 453c728a1e90a4bc03baf1c026a4bd4a4e9d97c5 | |
parent | 372d1b5a6444b2817007fd5c39c79fe6a6144b54 (diff) | |
download | etbsa-traccar-manager-android-ba9847a0df2a6ca7c1bb570ae8d808f2623687e7.tar.gz etbsa-traccar-manager-android-ba9847a0df2a6ca7c1bb570ae8d808f2623687e7.tar.bz2 etbsa-traccar-manager-android-ba9847a0df2a6ca7c1bb570ae8d808f2623687e7.zip |
Support URL redirection
-rw-r--r-- | app/src/main/java/org/traccar/manager/StartFragment.java | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/app/src/main/java/org/traccar/manager/StartFragment.java b/app/src/main/java/org/traccar/manager/StartFragment.java index c7d5838..fd0849a 100644 --- a/app/src/main/java/org/traccar/manager/StartFragment.java +++ b/app/src/main/java/org/traccar/manager/StartFragment.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,8 @@ public class StartFragment extends Fragment implements View.OnClickListener { private static final String TAG = StartFragment.class.getSimpleName(); + private static int MAX_REDIRECTS = 5; + private EditText serverField; private Button startButton; @@ -67,8 +69,24 @@ public class StartFragment extends Fragment implements View.OnClickListener { try { Uri uri = Uri.parse(urls[0]).buildUpon().appendEncodedPath("api/server").build(); - URL url = new URL(uri.toString()); - HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + String url = uri.toString(); + HttpURLConnection urlConnection = null; + + for (int i = 0; i < MAX_REDIRECTS; i++) { + URL resourceUrl = new URL(url); + urlConnection = (HttpURLConnection) resourceUrl.openConnection(); + urlConnection.setInstanceFollowRedirects(false); + + switch (urlConnection.getResponseCode()) { + case HttpURLConnection.HTTP_MOVED_PERM: + case HttpURLConnection.HTTP_MOVED_TEMP: + url = urlConnection.getHeaderField("Location"); + continue; + } + + break; + } + BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String line; |