Fix: Crash on HTML response in EventRepository and others
- Fixed: Value <! DOCTYPE cannot be converted to JSONArray in EventRepository - Fixed: Added HTML validation in UpdateManager and DeviceRegistry - Fixed: Improved HTTP error handling in StreamUrlResolver - Improved: Error messages in PlayerActivity - Bumped version to 9.4.3
This commit is contained in:
@@ -79,19 +79,51 @@ public class EventRepository {
|
||||
connection.setConnectTimeout(15000);
|
||||
connection.setReadTimeout(15000);
|
||||
connection.setRequestMethod("GET");
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
connection.setRequestProperty("User-Agent", "StreamPlayer/1.0");
|
||||
|
||||
try {
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||||
throw new IOException("Error HTTP " + responseCode + ": " + connection.getResponseMessage());
|
||||
}
|
||||
|
||||
String contentType = connection.getContentType();
|
||||
if (contentType != null && !contentType.contains("json")) {
|
||||
throw new IOException("El servidor devolvió " + contentType + " en lugar de JSON. Verifica que la URL sea correcta.");
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
String response = builder.toString();
|
||||
|
||||
// Validar que no sea HTML
|
||||
if (response.trim().startsWith("<!") || response.trim().startsWith("<html")) {
|
||||
throw new IOException("El servidor devolvió HTML en lugar de JSON. La URL del endpoint puede estar incorrecta o el servidor tiene problemas.");
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
return builder.toString();
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
private List<EventItem> parseEvents(String json) throws JSONException {
|
||||
if (json == null || json.trim().isEmpty()) {
|
||||
throw new JSONException("La respuesta está vacía");
|
||||
}
|
||||
|
||||
// Validar que no sea HTML antes de parsear
|
||||
String trimmed = json.trim();
|
||||
if (trimmed.startsWith("<!") || trimmed.startsWith("<html")) {
|
||||
throw new JSONException("Se recibió HTML en lugar de JSON");
|
||||
}
|
||||
|
||||
JSONArray array = new JSONArray(json);
|
||||
List<EventItem> events = new ArrayList<>();
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
|
||||
Reference in New Issue
Block a user