1
\$\begingroup\$

This is within a base class of a library, where several other classes inherit this one.

This seems like the simplest way of just getting some data, for now without timeout/errorhandling. This is supposed to get some data that will most ofthe time be converted to a small JSONObject.

Do you think I should put everything inside the try and use val? Any other suggestions? I'm basically just learning networking with android, only did it in C#.NET before.

protected fun sendGetRequest(url: URL): String {
 var urlConnection: HttpsURLConnection? = null
 var reader: InputStreamReader? = null
 var responseData = ""
 try {
 urlConnection = url.openConnection() as HttpsURLConnection
 reader = InputStreamReader(urlConnection.inputStream)
 responseData = reader.readText()
 }catch (e: Exception){
 this.messageHandler.handleMessage(e.message!!)
 }finally {
 reader?.close()
 urlConnection?.disconnect()
 }
 return responseData
}
200_success
146k22 gold badges190 silver badges479 bronze badges
asked Sep 22, 2019 at 10:29
\$\endgroup\$
2
  • 1
    \$\begingroup\$ But you are doing error handling. What does this.messageHandler do? \$\endgroup\$ Commented Sep 22, 2019 at 14:10
  • \$\begingroup\$ It propagates messages to the outside of the library. Right now the app using it just shows the message in a toast. With error handling I mean actually handling http codes and such. \$\endgroup\$ Commented Sep 22, 2019 at 14:26

1 Answer 1

1
\$\begingroup\$

The core of your code can be replaced by the following code:

url.openConnection() //you can cast to fail if it isn't https
 .getInputStream() //same as you
 .reader() // same as wrapping it in inputStreamReader
 .use { it -> // start lambda that handles the closing of the reader for you
 it.readText() // note, it's one parameter, so 'it ->' can be omitted
 }

The code above returns the text or throws the exception it got during execution, but always closes the reader, which in turn closes the connection.

The code above is almost the same as the code below btw:

url.readText()

If you want to remove all the variables, you should know that try catch can return a value:

val a = try{ 1 } catch(e: Exception) { 2 }
answered Sep 24, 2019 at 10:19
\$\endgroup\$
4
  • \$\begingroup\$ So if I handle possible exceptions somewhere else, basically the whole thing I made is just url.readText()? That is quite simple. I'm glad I asked. \$\endgroup\$ Commented Sep 24, 2019 at 12:03
  • \$\begingroup\$ yup. In the documentation, Kotlin tells you not to use it for huge files. The reason is because the text will be stored into a String, so into memory. This means it can almost always be ignored. \$\endgroup\$ Commented Sep 24, 2019 at 14:00
  • \$\begingroup\$ In android you could theoretically store around 4 million characters. Just a little bit more than the letters of the bible :-) \$\endgroup\$ Commented Sep 24, 2019 at 14:06
  • 1
    \$\begingroup\$ Oh I expect a small json object most of the time, and an error page or nothing on error. Thanks for the help :D \$\endgroup\$ Commented Sep 24, 2019 at 14:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.