Contenidos
Fichero cumple.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[ { "nombre": "Juan", "fecha_nacimiento": "1990-12-18" }, { "nombre": "María", "fecha_nacimiento": "1985-09-28" }, { "nombre": "Pedro", "fecha_nacimiento": "1995-02-10" }, { "nombre": "Ana", "fecha_nacimiento": "1988-12-03" }, { "nombre": "Luisa", "fecha_nacimiento": "1976-12-18" } ] |
Añadir las siguiente dependencias al fichero build.gradle.kts
1 2 |
implementation("com.squareup.okhttp3:okhttp:4.9.3") implementation("com.google.code.gson:gson:2.8.8") |
Añadir los permisos al fichero AndroidManifest.xml
1 2 |
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
package com.example.myapplication import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* import androidx.compose.material3.Text import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import com.google.gson.Gson import com.google.gson.reflect.TypeToken import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import java.io.IOException import java.text.SimpleDateFormat import java.util.* class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { BirthdayApp() } } } @Composable fun BirthdayApp() { val context = LocalContext.current var personsWithBirthdayToday by remember { mutableStateOf<List<Person>>(emptyList()) } LaunchedEffect(true) { personsWithBirthdayToday = withContext(Dispatchers.IO) { findPeopleWithBirthdayToday(context) } } Column( modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { Text("Personas con cumpleaños hoy:") personsWithBirthdayToday.forEach { person -> Text(person.nombre) } } } fun findPeopleWithBirthdayToday(context: android.content.Context): List<Person> { val jsonString = readJsonFromUrl("https://www.jesusninoc.com/cumple.json") val gson = Gson() val listType = object : TypeToken<List<Person>>() {}.type val persons: List<Person> = gson.fromJson(jsonString, listType) val currentDate = Calendar.getInstance().time val dateFormatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) val peopleWithBirthdayToday = mutableListOf<Person>() persons.forEach { person -> val birthDate = dateFormatter.parse(person.fecha_nacimiento) if (birthDate != null && isSameDayAndMonth(birthDate, currentDate)) { peopleWithBirthdayToday.add(person) } } return peopleWithBirthdayToday } fun readJsonFromUrl(url: String): String { val client = OkHttpClient() val request = Request.Builder() .url(url) .build() val response: Response try { response = client.newCall(request).execute() return response.body?.string() ?: "" } catch (e: IOException) { e.printStackTrace() return "" } } fun isSameDayAndMonth(date1: Date, date2: Date): Boolean { val cal1 = Calendar.getInstance().apply { time = date1 } val cal2 = Calendar.getInstance().apply { time = date2 } val dateFormatter = SimpleDateFormat("dd-MM", Locale.getDefault()) val dayAndMonth1 = dateFormatter.format(cal1.time) val dayAndMonth2 = dateFormatter.format(cal2.time) return dayAndMonth1 == dayAndMonth2 } data class Person( val nombre: String, val fecha_nacimiento: String ) |