Android Networking: Fundamentals

Sep 6 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk | 2021.2.1 Patch 1

Part 1: Learn About HTTP & Threading

05. Parse Data as JSON

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 04. Create a HTTP Connection Next episode: 06. Challenge: Use HttpUrlConnection

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 05. Parse Data as JSON

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

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

As you have seen, the response you received from the server was in the JSON format. And to show it to the user, you need to parse the message from the JSON object.

private val networkStatusChecker by lazy {
  NetworkStatusChecker(getSystemService(ConnectivityManager::class.java))
}
networkStatusChecker.performIfConnectedToInternet {
  runOnUiThread {
    ...
  }
}
Thread(Runnable {
  val connection = URL("$BASE_URL/api/login").openConnection() as HttpURLConnection
  connection.requestMethod = "POST"
  connection.setRequestProperty("Content-Type", "application/json")
  connection.setRequestProperty("Accept", "application/json")
  connection.readTimeout = 10000
  connection.connectTimeout = 10000
  connection.doOutput = true
  connection.doInput = true

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

  val body = "{\"email\":\"${userDataRequest.email}\", " + "\"password\":\"${userDataRequest.password}\"}"

  val bytes = body.toByteArray()

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

  val body = "{\"email\":\"${userDataRequest.email}\", " + "\"password\":\"${userDataRequest.password}\"}"

  val bytes = body.toByteArray()

  try {
    connection.outputStream.use { outputStream ->
      outputStream.write(bytes)
    }

    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/login").openConnection() as HttpURLConnection
  connection.requestMethod = "POST"
  connection.setRequestProperty("Content-Type", "application/json")
  connection.setRequestProperty("Accept", "application/json")
  connection.readTimeout = 10000
  connection.connectTimeout = 10000
  connection.doOutput = true
  connection.doInput = true

  val body = "{\"email\":\"${userDataRequest.email}\", " + "\"password\":\"${userDataRequest.password}\"}"

  val bytes = body.toByteArray()

  try {
    connection.outputStream.use { outputStream ->
      outputStream.write(bytes)
    }

    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 jsonObject = JSONObject(response.toString())

      onUserLoggedIn(jsonObject.getString("token"), null)
    }
  } catch (error: Throwable) {
    onUserLoggedIn(null, error)
  }

  connection.disconnect()
}).start()
val requestJson = JSONObject()
requestJson.put("email", userDataRequest.email)
requestJson.put("password", userDataRequest.password)
val body = requestJson.toString()
val requestJson = JSONObject()
requestJson.put("email", userDataRequest.email)
requestJson.put("password", userDataRequest.password)
val body = requestJson.toString()
val jsonObject = JSONObject(response.toString())

onUserCreated(jsonObject.getString("message"), null)