Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Parse JSON Data

The student materials have been reviewed and are updated as of July 2022.

Learn about JSON structures and how they correlate to Kotlin code. Use the Gson parser for automatic parsing.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

After practicting parsing the data manually, it’s time to see how you can do it automatically, using a library called GSON.

implementation 'com.google.code.gson:gson:2.8.6'
private val networkStatusChecker by lazy {
  NetworkStatusChecker(activity?.getSystemService(ConnectivityManager::class.java))
}
networkStatusChecker.performIfConnectedToInternet {
  activity?.runOnUiThread {
    ...
  }
}
private val gson = Gson()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/note").openConnection() as HttpURLConnection
  connection.requestMethod = "GET"
  connection.setRequestProperty("Content-Type", "application/json")
  connection.setRequestProperty("Accept", "application/json")
  connection.setRequestProperty("Authorization", App.getToken())
  connection.readTimeout = 10000
  connection.connectTimeout = 10000
  connection.doInput = true

  connection.disconnect()
}).start()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/note").openConnection() as HttpURLConnection
  connection.requestMethod = "GET"
  connection.setRequestProperty("Content-Type", "application/json")
  connection.setRequestProperty("Accept", "application/json")
  connection.setRequestProperty("Authorization", App.getToken())
  connection.readTimeout = 10000
  connection.connectTimeout = 10000
  connection.doInput = true

  try {
    val reader = InputStreamReader(connection.inputStream)

    reader.use { input ->
      val response = StringBuilder()
      val bufferedReader = BufferedReader(input)

      bufferedReader.useLines { lines ->
        lines.forEach {
          response.append(it.trim())
        }
      }
    }
  } catch (error: Throwable) {

  }

  connection.disconnect()
}).start()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/note").openConnection() as HttpURLConnection
  connection.requestMethod = "GET"
  connection.setRequestProperty("Content-Type", "application/json")
  connection.setRequestProperty("Accept", "application/json")
  connection.setRequestProperty("Authorization", App.getToken())
  connection.readTimeout = 10000
  connection.connectTimeout = 10000
  connection.doInput = true

  try {
    val reader = InputStreamReader(connection.inputStream)

    reader.use { input ->
      val response = StringBuilder()
      val bufferedReader = BufferedReader(input)

      bufferedReader.useLines { lines ->
        lines.forEach {
          response.append(it.trim())
        }
      }

      val tasksResponse = gson.fromJson(response.toString(), GetTasksResponse::class.java)
      onTasksReceived(tasksResponse.notes, null)
    }
  } catch (error: Throwable) {
    onTasksReceived(emptyList(), error)
  }

  connection.disconnect()
}).start()
val request = gson.toJson(addTaskRequest)
...
val body = gson.toJson(userDataRequest) // login & register

val task = gson.fromJson(response.toString(), Task::class.java)

Reviews

Comments