Android with Kotlin - How to use HttpUrlConnection

I am trying to get data from a url inside AsyncTask

but I am getting an error when creating a new instance HttpUrlConnection

.

Something like this in Java

URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    readStream(in);
finally {
    urlConnection.disconnect();
}

      

But I keep getting the error shown below.

class GetWeatherTask : AsyncTast<Void, Void, Void>() {

    override fun doInBackground(vararg params: Void?): Void? {
        val httpClient = HttpURLConnection();
        return null
    }
    override fun onPreExecute() {
        super.onPreExecute()
    }
    override fun onPostExecute(result: Void?) {
        super.onPostExecute(result)
    }
}

      

Unable to access' ': it is protected / protected and package /' in 'HttpURLConnection' Cannot instantiate abstract class

Am I missing something? I tried to create a class object extending HttpUrlConnection

and try to implement the method init

but I could not

Thanks in advance.

+3


source to share


1 answer


Here is a simplification of the question and answer.

Why does it fail?

val connection = HttpURLConnection()
val data = connection.inputStream.bufferedReader().readText()
// ... do something with "data"

      

with error:

Kotlin: Cannot access' ': it is protected / protected and package /' in 'HttpURLConnection'

It fails because you are creating a class that is not intended to be directly built. It is for creating a factory that is in the URL

class method openConnection()

. It is also not a direct port of the Java code sample in the original question.

The most idiomatic way in Kotlin to open this connection and read the content as a string:

val connection = URL("http://www.android.com/").openConnection() as HttpURLConnection
val data = connection.inputStream.bufferedReader().readText()

      



This form automatically closes everything when it reads text or an exception. If you want to do a custom read:

val connection = URL("http://www.android.com/").openConnection() as HttpURLConnection
connection.inputStream.bufferedReader().use { reader ->
    // ... do something with the reader
}

      

NOTE. the extension function use()

will open and close the reader and automatically close errors.

About method disconnect()

The docs for disconnect

say:

Each HttpURLConnection instance is used to create a single request, but the underlying network connection to the HTTP server can be transparently shared by other instances. Calling the close () methods on an InputStream or OutputStream of an HttpURLConnection connection after a request can release the network resources associated with it but does not affect any shared persistent connection. Calling the disconnect () method can close the underlying socket if the persistent connection is not running at the time.

So, you decide if you want to call it or not. Here is the version of the code that causes the disconnect:

val connection = URL("http://www.android.com/").openConnection() as HttpURLConnection
try {
    val data = connection.inputStream.bufferedReader().readText()
    // ... do something with "data"
} finally {
    connection.disconnect()
}

      

+9


source







All Articles