I have an app that previously read an xml file from a server and saved it in the phone so that the user could see the content without internet connection, I have passed my application to kotlin and I find the error that the file no longer save it or write it in memory, previously I used this function in an Asyntask but where is the while I get an error and I could not solve it, thanks.
val conexion: HttpsURLConnection
val urlArchivo: URL
val url1 = "Mi URL"
val fileOut: FileOutputStream
val buffersize = 1024
val buffer: ByteArray
try {
urlArchivo = URL(url1)
conexion = urlArchivo.openConnection() as HttpsURLConnection
conexion.readTimeout = 1000
conexion.connectTimeout = 1000
conexion.doInput = true
conexion.requestMethod = "GET"
conexion.connect()
ruta = conexion.inputStream
fileOut = FileOutputStream(archivo) // ubicacion archivo
var bytesEscribir: Int
buffer = ByteArray(buffersize)
while ((bytesEscribir = ruta.read(buffer)) > 0) {
fileOut.write(buffer, 0, bytesEscribir)
}
Toast.makeText(activity!!.applicationContext, getString(R.string.sinc_exitosa), Toast.LENGTH_LONG).show()
ruta.close()
fileOut.close()
conexion.disconnect()