AndroidManifest.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.MyApplication"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/loadButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cargar JSON" /> <TextView android:id="@+id/jsonTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Contenido JSON:" android:layout_below="@id/loadButton" android:layout_centerHorizontal="true" android:layout_marginTop="20dp"/> </RelativeLayout> |
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 |
package com.example.myapplication import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.activity.ComponentActivity import com.android.volley.Request import com.android.volley.Response import com.android.volley.toolbox.StringRequest import com.android.volley.toolbox.Volley class MainActivity : ComponentActivity() { private lateinit var jsonTextView: TextView private val jsonUrl = "https://www.jesusninoc.com/rutinas2.json" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) jsonTextView = findViewById(R.id.jsonTextView) val loadButton: Button = findViewById(R.id.loadButton) loadButton.setOnClickListener { loadJSONFromUrl(jsonUrl) } } private fun loadJSONFromUrl(url: String) { val queue = Volley.newRequestQueue(this) val stringRequest = StringRequest(Request.Method.GET, url, Response.Listener<String> { response -> jsonTextView.text = response }, Response.ErrorListener { error -> error.printStackTrace() }) queue.add(stringRequest) } } |
