Android with Kotlin - How to use HttpUrlConnection -


i´m trying data url inside asynctask error when creating new instance of httpurlconnection.

something on java

url url = new url("http://www.android.com/"); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); try {     inputstream in = new bufferedinputstream(urlconnection.getinputstream());     readstream(in); {     urlconnection.disconnect(); } 

but keep getting 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)     } } 

cannot access '': 'protected/protected , package/' in 'httpurlconnection' cannot create instance of abstract class

am missing something? tryied create class object extending httpurlconnection , try implement init method couldn't

thanks in advance.

here simplification of question , answer.

why fail?

val connection = httpurlconnection() val data = connection.inputstream.bufferedreader().readtext() // ... "data" 

with error:

kotlin: cannot access '': 'protected/protected , package/' in 'httpurlconnection'

this fails because constructing class not intended directly constructed. meant created factory, in url class openconnection() method. not direct port of sample java code in original question.

the idiomatic way in kotlin open connection , read contents string be:

val connection = url("http://www.android.com/").openconnection() httpurlconnection val data = connection.inputstream.bufferedreader().readtext() 

this form auto close when done reading text or on exception. if want custom reading:

val connection = url("http://www.android.com/").openconnection() httpurlconnection connection.inputstream.bufferedreader().use { reader ->     // ... reader } 

note: the use() extension function open , close reader , handle closing on errors automatically.

about disconnect() method

the docs disconnect say:

each httpurlconnection instance used make single request underlying network connection http server may transparently shared other instances. calling close() methods on inputstream or outputstream of httpurlconnection after request may free network resources associated instance has no effect on shared persistent connection. calling disconnect() method may close underlying socket if persistent connection otherwise idle @ time.

so decide if want call or not. here version of code calls disconnect:

val connection = url("http://www.android.com/").openconnection() httpurlconnection try {     val data = connection.inputstream.bufferedreader().readtext()     // ... "data" } {     connection.disconnect() } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -