0

I copied over some Java code and had it converted to Kotlin code. It compiles and runs, but it is unable to load online text and set that text to a textview. I get a lot of permission denied statements when checking on the status of the app while it is running.

package com.example.app

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.net.URLConnection
import java.net.URL
import java.util.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var textBody: TextView = findViewById(R.id.textBody)

        var content: String? = null
        var connection: URLConnection? = null
        try {
            connection = URL("https://example.com").openConnection()
            val scanner = Scanner(connection.getInputStream())
            content = scanner.next()
            scanner.close()
        } catch (ex: Exception) {
            ex.printStackTrace()
        }
       textBody.setText(content)


    }
}
MVVV
  • 28
  • 5
  • 1
    That code is fairly ancient and was not designed with Android in mind. You might want to focus on newer educational resources. FWIW, [all my books are free now](https://commonsware.com/catalog), and they cover basic Android app development in Kotlin. This includes how to load content from Web servers. – CommonsWare Apr 17 '22 at 23:49
  • check out this solution might help full https://stackoverflow.com/a/17141903/7706832 – Maulik Togadiya Apr 18 '22 at 02:44

0 Answers0