{"id":522,"date":"2023-11-26T14:17:59","date_gmt":"2023-11-26T14:17:59","guid":{"rendered":"https:\/\/javigomez.org\/?p=522"},"modified":"2025-09-29T20:33:55","modified_gmt":"2025-09-29T20:33:55","slug":"10-01b-kotlin-retrofit","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2023\/11\/26\/10-01b-kotlin-retrofit\/","title":{"rendered":"10.01b. Kotlin- Retrofit"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code><strong>AndroidManifest.xml<\/strong>\n\n&lt;uses-permission android:name=\"android.permission.INTERNET\"\/><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Gradle (Module:app)<\/strong>\n\n\/\/Retrofit\nimplementation (\"com.squareup.retrofit2:retrofit:2.9.0\")\nimplementation (\"com.squareup.retrofit2:converter-gson:2.9.0\")\n<\/code><\/pre>\n\n\n\n<p>Crear un data class<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>UsuariosResponse.kt<\/strong>\n\npackage com.javi.u10a\n\ndata class Usuario(\n    val id: String,\n    val nombre: String,\n    val apellido1: String,\n    val apellido2: String,\n    val URL:String\n)\n<\/code><\/pre>\n\n\n\n<p>Crear una clase interface<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>ApiService.kt<\/strong>\n\npackage com.javi.u10a\n\nimport retrofit2.http.GET\nimport retrofit2.http.Query\n\ninterface ApiService {\n    @GET(\"findByID.php\")\n    suspend fun findByID(@Query(\"id\") id: Int): List&lt;Usuario>\n}\n\n<\/code><\/pre>\n\n\n\n<p>En el MainActivity.kt<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.javi.u10a\n\nimport androidx.appcompat.app.AppCompatActivity\nimport android.os.Bundle\nimport android.util.Log\nimport com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory\nimport kotlinx.coroutines.CoroutineScope\nimport kotlinx.coroutines.Dispatchers\nimport kotlinx.coroutines.launch\nimport retrofit2.Retrofit\nimport retrofit2.converter.gson.GsonConverterFactory\n\n\nclass MainActivity : AppCompatActivity() {\n\n<strong>    private val apiService: ApiService by lazy {\n        val retrofit = Retrofit.Builder()\n            .baseUrl(&quot;https:\/\/javigomez.org\/ESIC\/PMDM\/%22\n            .addConverterFactory(GsonConverterFactory.create())\n            .addCallAdapterFactory(CoroutineCallAdapterFactory())\n            .build()\n\n        retrofit.create(ApiService::class.java)\n    }<\/strong>\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n<strong>        CoroutineScope(Dispatchers.IO).launch {\n            try {\n                val usuarios = apiService.findByID(1)\n                 if (usuarios.isNotEmpty()) {\n                    val usuario = usuarios&#91;0]\n                    Log.d(\"JAI\", \"Usuario: ${usuario.id}, ${usuario.nombre}, ${usuario.apellido1}, ${usuario.apellido2}\")\n                }\n            } catch (e: Exception) {\n                Log.e(\"JAI\", \"Error al obtener usuarios\", e)\n            }\n        }<\/strong>\n    }\n}\n\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Kotlin U10 01b Retrofit\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/WKn0V4iwVC0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Crear un data class Crear una clase interface En el MainActivity.kt<\/p>\n","protected":false},"author":1,"featured_media":347,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[34,100,212,290],"class_list":["post-522","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin","tag-api","tag-coroutine","tag-kotlin","tag-retrofit"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=522"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/522\/revisions"}],"predecessor-version":[{"id":918,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/522\/revisions\/918"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media\/347"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}