fix: implementar sistema de fallback y redirecciones HTTP para carga de eventos
- Agregar sistema de fallback con múltiples URLs (streamtpcloud.com, streamtp10.com, streamtpmedia.com) - Implementar seguimiento automático de redirecciones HTTP (301, 302, 303, 307, 308) - Guardar última URL exitosa en SharedPreferences para optimizar futuras peticiones - Corregir error "Unable to resolve host 'streamtpcloud.com'" cuando el dominio cambia Resuelve issue donde los eventos no cargaban debido a cambios en el dominio del servidor. La app ahora se adapta automáticamente sin necesidad de actualización.
This commit is contained in:
26
CHANGELOG-v10.1.3.md
Normal file
26
CHANGELOG-v10.1.3.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# StreamPlayer v10.1.3
|
||||||
|
|
||||||
|
## Cambios en esta versión
|
||||||
|
|
||||||
|
### Corrección de Carga de Eventos
|
||||||
|
|
||||||
|
- **Sistema de fallback con múltiples URLs**: Implementado sistema inteligente que intenta múltiples URLs de eventos cuando la principal no está disponible:
|
||||||
|
- `https://streamtpcloud.com/eventos.json` (URL original)
|
||||||
|
- `https://streamtp10.com/eventos.json` (URL actual)
|
||||||
|
- `https://streamtpmedia.com/eventos.json` (URL anterior)
|
||||||
|
|
||||||
|
- **Seguimiento automático de redirecciones HTTP**: El cliente ahora sigue automáticamente las redirecciones HTTP (códigos 301, 302, 303, 307, 308), lo que permite adaptarse a cambios de URL del servidor sin necesidad de actualizar la app.
|
||||||
|
|
||||||
|
- **Memoria de URL exitosa**: La app recuerda cuál fue la última URL que funcionó correctamente y la intenta primero en futuras peticiones, mejorando el rendimiento y la fiabilidad.
|
||||||
|
|
||||||
|
### Detalles Técnicos
|
||||||
|
|
||||||
|
- Modificado `EventRepository.java` para implementar:
|
||||||
|
- Lógica de reintento secuencial con múltiples URLs
|
||||||
|
- Seguimiento manual de redirecciones (hasta 5 consecutivas)
|
||||||
|
- Persistencia de la última URL exitosa en SharedPreferences
|
||||||
|
- Manejo mejorado de errores con mensajes descriptivos
|
||||||
|
|
||||||
|
### Problema Resuelto
|
||||||
|
|
||||||
|
Esta versión corrige el error: *"Unable to resolve host 'streamtpcloud.com': No address associated with hostname"* que ocurría cuando el servidor de eventos cambió su dominio. La app ahora se adapta automáticamente a estos cambios sin intervención del usuario.
|
||||||
@@ -29,8 +29,17 @@ public class EventRepository {
|
|||||||
private static final String PREFS_NAME = "events_cache";
|
private static final String PREFS_NAME = "events_cache";
|
||||||
private static final String KEY_JSON = "json";
|
private static final String KEY_JSON = "json";
|
||||||
private static final String KEY_TIMESTAMP = "timestamp";
|
private static final String KEY_TIMESTAMP = "timestamp";
|
||||||
|
private static final String KEY_WORKING_URL = "working_url";
|
||||||
private static final long CACHE_DURATION = 24L * 60 * 60 * 1000; // 24 horas
|
private static final long CACHE_DURATION = 24L * 60 * 60 * 1000; // 24 horas
|
||||||
private static final String EVENTS_URL = "https://streamtpcloud.com/eventos.json";
|
|
||||||
|
// Lista de URLs a intentar en orden (con sistema de fallback)
|
||||||
|
private static final String[] EVENT_URLS = {
|
||||||
|
"https://streamtpcloud.com/eventos.json", // URL original
|
||||||
|
"https://streamtp10.com/eventos.json", // URL actual
|
||||||
|
"https://streamtpmedia.com/eventos.json" // URL anterior
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final String DEFAULT_EVENTS_URL = "https://streamtpcloud.com/eventos.json";
|
||||||
|
|
||||||
public interface Callback {
|
public interface Callback {
|
||||||
void onSuccess(List<EventItem> events);
|
void onSuccess(List<EventItem> events);
|
||||||
@@ -55,7 +64,7 @@ public class EventRepository {
|
|||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
String json = downloadJson();
|
String json = downloadJson(context);
|
||||||
List<EventItem> events = parseEvents(json);
|
List<EventItem> events = parseEvents(json);
|
||||||
prefs.edit().putString(KEY_JSON, json).putLong(KEY_TIMESTAMP, System.currentTimeMillis()).apply();
|
prefs.edit().putString(KEY_JSON, json).putLong(KEY_TIMESTAMP, System.currentTimeMillis()).apply();
|
||||||
callback.onSuccess(events);
|
callback.onSuccess(events);
|
||||||
@@ -73,8 +82,44 @@ public class EventRepository {
|
|||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String downloadJson() throws IOException {
|
private String downloadJson(Context context) throws IOException {
|
||||||
URL url = new URL(EVENTS_URL);
|
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||||
|
String savedWorkingUrl = prefs.getString(KEY_WORKING_URL, null);
|
||||||
|
|
||||||
|
// Construir lista de URLs a intentar
|
||||||
|
// Primero la URL que funcionó la última vez, luego el resto
|
||||||
|
List<String> urlsToTry = new ArrayList<>();
|
||||||
|
if (savedWorkingUrl != null && !savedWorkingUrl.isEmpty()) {
|
||||||
|
urlsToTry.add(savedWorkingUrl);
|
||||||
|
}
|
||||||
|
for (String url : EVENT_URLS) {
|
||||||
|
if (!urlsToTry.contains(url)) {
|
||||||
|
urlsToTry.add(url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IOException lastException = null;
|
||||||
|
|
||||||
|
// Intentar cada URL en orden
|
||||||
|
for (String urlString : urlsToTry) {
|
||||||
|
try {
|
||||||
|
String json = downloadFromUrl(urlString);
|
||||||
|
// Guardar la URL que funcionó
|
||||||
|
prefs.edit().putString(KEY_WORKING_URL, urlString).apply();
|
||||||
|
return json;
|
||||||
|
} catch (IOException e) {
|
||||||
|
lastException = e;
|
||||||
|
// Continuar con la siguiente URL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si todas fallaron, lanzar la última excepción
|
||||||
|
throw new IOException("No se pudo conectar a ninguna de las URLs disponibles. Último error: " +
|
||||||
|
(lastException != null ? lastException.getMessage() : "Error desconocido"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String downloadFromUrl(String urlString) throws IOException {
|
||||||
|
URL url = new URL(urlString);
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setConnectTimeout(15000);
|
connection.setConnectTimeout(15000);
|
||||||
connection.setReadTimeout(15000);
|
connection.setReadTimeout(15000);
|
||||||
@@ -82,8 +127,48 @@ public class EventRepository {
|
|||||||
connection.setRequestProperty("Accept", "application/json");
|
connection.setRequestProperty("Accept", "application/json");
|
||||||
connection.setRequestProperty("User-Agent", "StreamPlayer/1.0");
|
connection.setRequestProperty("User-Agent", "StreamPlayer/1.0");
|
||||||
|
|
||||||
|
// Habilitar seguimiento de redirecciones automáticamente
|
||||||
|
connection.setInstanceFollowRedirects(true);
|
||||||
|
|
||||||
|
String currentUrl = urlString;
|
||||||
|
int redirectCount = 0;
|
||||||
|
final int MAX_REDIRECTS = 5;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int responseCode = connection.getResponseCode();
|
int responseCode = connection.getResponseCode();
|
||||||
|
|
||||||
|
// Seguir redirecciones manualmente si es necesario
|
||||||
|
while (isRedirect(responseCode) && redirectCount < MAX_REDIRECTS) {
|
||||||
|
redirectCount++;
|
||||||
|
String newUrl = connection.getHeaderField("Location");
|
||||||
|
|
||||||
|
if (newUrl == null) {
|
||||||
|
throw new IOException("Redirección sin cabecera Location");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Manejar URLs relativas
|
||||||
|
if (newUrl.startsWith("/")) {
|
||||||
|
newUrl = url.getProtocol() + "://" + url.getHost() + newUrl;
|
||||||
|
} else if (!newUrl.startsWith("http")) {
|
||||||
|
newUrl = url.getProtocol() + "://" + url.getHost() +
|
||||||
|
(url.getPort() > 0 ? ":" + url.getPort() : "") + "/" + newUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentUrl = newUrl;
|
||||||
|
url = new URL(currentUrl);
|
||||||
|
connection.disconnect();
|
||||||
|
|
||||||
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setConnectTimeout(15000);
|
||||||
|
connection.setReadTimeout(15000);
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setRequestProperty("Accept", "application/json");
|
||||||
|
connection.setRequestProperty("User-Agent", "StreamPlayer/1.0");
|
||||||
|
connection.setInstanceFollowRedirects(true);
|
||||||
|
|
||||||
|
responseCode = connection.getResponseCode();
|
||||||
|
}
|
||||||
|
|
||||||
if (responseCode != HttpURLConnection.HTTP_OK) {
|
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||||||
throw new IOException("Error HTTP " + responseCode + ": " + connection.getResponseMessage());
|
throw new IOException("Error HTTP " + responseCode + ": " + connection.getResponseMessage());
|
||||||
}
|
}
|
||||||
@@ -114,6 +199,14 @@ public class EventRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isRedirect(int statusCode) {
|
||||||
|
return statusCode == HttpURLConnection.HTTP_MOVED_PERM ||
|
||||||
|
statusCode == HttpURLConnection.HTTP_MOVED_TEMP ||
|
||||||
|
statusCode == HttpURLConnection.HTTP_SEE_OTHER ||
|
||||||
|
statusCode == 307 || // Temporary Redirect
|
||||||
|
statusCode == 308; // Permanent Redirect
|
||||||
|
}
|
||||||
|
|
||||||
private List<EventItem> parseEvents(String json) throws JSONException {
|
private List<EventItem> parseEvents(String json) throws JSONException {
|
||||||
if (json == null || json.trim().isEmpty()) {
|
if (json == null || json.trim().isEmpty()) {
|
||||||
throw new JSONException("La respuesta está vacía");
|
throw new JSONException("La respuesta está vacía");
|
||||||
|
|||||||
Reference in New Issue
Block a user