AnswerBun.com

http url connection with kotlin on android

I’m making an http call from an android app and want to have it reviewed for brevity and efficiency. It handles both success and error streams, by contract 200 OK is enforced (not 201 created other codes etc.)

class UserNetwork() {

    fun findAll(): ArrayList<User>? {
        val url = URL("http:///10.0.2.2:8080/employees")
        val connection = url.openConnection() as HttpURLConnection
        connection.setRequestProperty("Accept", "application/json");

        var users: ArrayList<User>? = null

        (if (connection.responseCode == 200) connection.inputStream else connection.errorStream).use { stream ->
            BufferedReader(InputStreamReader(stream)).use { reader ->
                val response = StringBuffer()
                users = arrayListOf()

                @Suppress("ControlFlowWithEmptyBody")
                while ((reader.readLine().also { response.append(it) }) != null) {}

                val body = JSONArray(response.toString())
                for (i in 0 until body.length()) {
                    users?.add(User(body.getJSONObject(i)))
                }
            }
        }

        return users
    }
}

User data object.

data class User(var id: Long? = null, var first: String? = null, var last: String? = null): Parcelable {

    constructor(data: JSONObject) : this() {
        id = data.getLong("id")
        first = data.getString("first")
        last = data.getString("last")
    }
}

Test class (testing method for findAll)

class UserNetworkTest {
    @Test
    fun testFindAll() {
        val users = userProxy.findAll()
        assertEquals(3, users!!.size)
        users.forEach { user ->
            assertNotNull(user.id)
            assertNotNull(user.first)
            assertNotNull(user.last)
        }
    }
}

Code Review Asked on December 29, 2020

0 Answers

Add your own answers!

Related Questions

Yet another reflection library

0  Asked on November 8, 2021 by vladyslav-mozhvylo

         

DynamicArray class in C++

3  Asked on November 8, 2021 by pythenx

     

JS + CANVAS Projectile Motion

1  Asked on November 5, 2021 by j-albert

       

Volunteer Signup Website Homepage

1  Asked on November 5, 2021 by reddragonwebdesign

   

Remove duplicated functions inside different if within a function?

1  Asked on October 27, 2021 by menai-ala-eddine-aladdin

 

An emotion recognition test I designed using Pygame

1  Asked on October 27, 2021 by theamazingone1

         

Add values from a dictionary to subarrays using numpy

1  Asked on October 27, 2021 by carlos-eduardo-corpus

     

Liquid includes Share Page

0  Asked on October 27, 2021 by s0ands0

       

Return payload in service layer

1  Asked on October 27, 2021

   

Pin and Password Generator JAVA

2  Asked on October 27, 2021 by brian-smithers

       

Hangman Bot built with performance in mind

3  Asked on October 27, 2021 by tajymany

       

C#: A* pathfinding – performance and simplicity

2  Asked on October 27, 2021 by xamtos

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.