{"id":525,"date":"2023-11-26T14:21:23","date_gmt":"2023-11-26T14:21:23","guid":{"rendered":"https:\/\/javigomez.org\/?p=525"},"modified":"2025-09-29T20:33:44","modified_gmt":"2025-09-29T20:33:44","slug":"10-01c-kotlin-retrofit-mostrando-informacion-por-pantalla","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2023\/11\/26\/10-01c-kotlin-retrofit-mostrando-informacion-por-pantalla\/","title":{"rendered":"10.01c. Kotlin- Retrofit mostrando informaci\u00f3n por pantalla"},"content":{"rendered":"\n<p>En este ejemplo se recupera la informaci\u00f3n de una API, pero se muestra la informaci\u00f3n por pantalla en vez de en el Log.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>activity_main.xml<\/strong>\n\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"&gt;\n    &lt;LinearLayout\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:orientation=\"horizontal\"&gt;\n    &lt;TextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_margin=\"10dp\"\n        android:textSize=\"20sp\"\n        android:text=\"Identificador\" \/&gt;\n    &lt;EditText\n        android:id=\"@+id\/edtID\"\n        android:layout_width=\"150dp\"\n        android:layout_height=\"wrap_content\"\n        android:layout_margin=\"10dp\"\/&gt;\n    &lt;Button\n        android:id=\"@+id\/btnBusca\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_gravity=\"center\"\n        android:text=\"Buscar\"\/&gt;\n    &lt;\/LinearLayout&gt;\n    &lt;TextView\n        android:id=\"@+id\/tvResultado\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_margin=\"10dp\"\n        android:textSize=\"20sp\"\n        android:text=\"Resultado\"\/&gt;\n&lt;\/LinearLayout&gt;\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>MainActivity.kt<\/strong>\n\npackage com.javi.u10retrofitvideo\nimport \u2026\nclass MainActivity : AppCompatActivity() {\n    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            .build()\n        retrofit.create(ApiService::class.java)\n    }\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n<strong>        val btnBusca = findViewById&lt;Button&gt;(R.id.btnBusca)\n        val edtID = findViewById&lt;EditText&gt;(R.id.edtID)\n        val tvResultado=findViewById&lt;TextView&gt;(R.id.tvResultado)\n        btnBusca.setOnClickListener {\n            val id = edtID.text.toString().toInt()<\/strong>\n            CoroutineScope(Dispatchers.IO).launch {\n                try {\n                    val usuarios = apiService.findByID(id)\n                    if (usuarios.isNotEmpty()) {\n                        val usuario = usuarios&#91;0]\n                        Log.d(\n                            \"JAI\",\n                            \"Usuario:${usuario.id}, ${usuario.nombre} ${usuario.apellido1},${usuario.apellido2}\"\n                        )\n<strong>                        runOnUiThread {\n                            tvResultado.text=\"Usuario:${usuario.id}, ${usuario.nombre} ${usuario.apellido1},${usuario.apellido2}\"\n                        }<\/strong>\n                    }\n                } catch (e: Exception) {\n                    Log.e(\"JAI\", \"Error al obtener usuarios\", e)\n                }\n            }\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 01c Retrofit View\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/cvOCIyc7KpA?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>En este ejemplo se recupera la informaci\u00f3n de una API, pero se muestra la informaci\u00f3n por pantalla en vez de en el Log.<\/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,254,290,294,344],"class_list":["post-525","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin","tag-api","tag-coroutine","tag-kotlin","tag-pantalla","tag-retrofit","tag-runonuithread","tag-view"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/525","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=525"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/525\/revisions"}],"predecessor-version":[{"id":917,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/525\/revisions\/917"}],"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=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}