OkHttp en Kotlin
Descarga de un fichero usando OkHttp en Kotlin
AndroidManifest.xml
Añadir el permiso de Internet
Añadir cleartext para poder usar http
<?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:usesCleartextTraffic="true"
build.gradle
Añadir viewBinding
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.okhttpkotlin'
compileSdk 33
buildFeatures {
viewBinding true
}
Añadir la biblioteca de okhttp a build.gradle (y sincronizar)
Release
dependencies {
implementation("com.squareup.okhttp3:okhttp:4.10.0")
}
strings.xml
<resources>
<string name="app_name">OkHttp en Kotlin</string>
<string name="web">https://dam.org.es/ficheros/frases.html</string>
<string name="descargar">Descargar</string>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="10dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/web"
android:textAppearance="@android:style/TextAppearance.Material.Large"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="344dp"
android:layout_height="41dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="@string/descargar"
app:layout_constraintBottom_toTopOf="@+id/webView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<WebView
android:id="@+id/webView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="79dp"
android:text="Tiempo de descarga"
android:textAppearance="@android:style/TextAppearance.Material.Large"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/webView" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.xml
class MainActivity : AppCompatActivity(), View.OnClickListener {
private lateinit var binding: ActivityMainBinding
var inicio: Long = 0
lateinit var url: URL
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.button.setOnClickListener(this)
}
override fun onClick(p0: View?) {
try {
url = URL(binding.editText.text.toString())
// descarga usando OkHttp
descargaOkHTTP(url)
} catch (e: MalformedURLException) {
e.printStackTrace()
mostrarError(e.message)
} catch (ex: IOException) {
mostrarError(ex.message)
}
}
private fun mostrarError(message: String?) {
Toast.makeText(this, "Error: $message", Toast.LENGTH_SHORT).show()
}
private fun descargaOkHTTP(web: URL) {
inicio = System.currentTimeMillis()
//https://square.github.io/okhttp/recipes/#asynchronous-get-kt-java
val client = OkHttpClient()
val request = Request.Builder()
.url(web)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("Error: ", e.message!!)
mostrarRespuesta("Fallo: " + e.message)
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
try {
if (!response.isSuccessful) {
//throw new IOException("Unexpected code " + response);
mostrarRespuesta("Unexpected code: <br>$response")
} else {
// Read data on the worker thread
//https://kotlinlang.org/docs/null-safety.html#the-operator
val responseData = response.body!!.string()
//Toast.makeText(getApplicationContext(), datos, Toast.LENGTH_SHORT).show();
mostrarRespuesta(responseData)
}
} catch (e: Error) {
Log.e("Error", "Error when executing get request: " + e.localizedMessage)
}
}
})
}
private fun mostrarRespuesta(message: String) {
var fin = System.currentTimeMillis()
// Run view-related code back on the main thread
runOnUiThread {
binding.webView.loadDataWithBaseURL(url.toString(), message, "text/html", "UTF-8", null)
binding.textView.text = "Duración: " + (fin - inicio).toString() + " milisegundos"
}
}
}

Deja una respuesta
Lo siento, debes estar conectado para publicar un comentario.