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.

Challenge: Use HttpUrlConnection

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

Create another HTTP request using the HttpURLConnection, to practice your skills. Parse the JSON and connect the response to the UI.

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.

The best way to make sure you’ve got a good understanding of creating requests and parsing the data is to pratice it on an example. In this challenge, you have to do two things.

if (App.getToken().isNotBlank()) {
  startActivity(MainActivity.getIntent(this))
}
private val networkStatusChecker by lazy {
  NetworkStatusChecker(activity?.getSystemService(ConnectivityManager::class.java))
}
networkStatusChecker.performIfConnectedToInternet {
  activity?.runOnUiThread {
    ...
  }
}
Thread(Runnable {

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

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

  val request = JSONObject()
  request.put("title", addTaskRequest.title)
  request.put("content", addTaskRequest.content)
  request.put("taskPriority", addTaskRequest.taskPriority)
  
  connection.disconnect()
}).start()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/note").openConnection() as HttpURLConnection
  connection.requestMethod = "POST"
  connection.setRequestProperty("Content-Type", "application/json")
  connection.setRequestProperty("Accept", "application/json")
  connection.setRequestProperty("Authorization", App.getToken())
  connection.readTimeout = 10000
  connection.connectTimeout = 10000
  connection.doOutput = true
  connection.doInput = true

  val request = JSONObject()
  request.put("title", addTaskRequest.title)
  request.put("content", addTaskRequest.content)
  request.put("taskPriority", addTaskRequest.taskPriority)

  try {
    connection.outputStream.use { outputStream ->
      outputStream.write(request.toString().toByteArray())
    }

    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 = "POST"
  connection.setRequestProperty("Content-Type", "application/json")
  connection.setRequestProperty("Accept", "application/json")
  connection.setRequestProperty("Authorization", App.getToken())
  connection.readTimeout = 10000
  connection.connectTimeout = 10000
  connection.doOutput = true
  connection.doInput = true

  val request = JSONObject()
  request.put("title", addTaskRequest.title)
  request.put("content", addTaskRequest.content)
  request.put("taskPriority", addTaskRequest.taskPriority)

  try {
    connection.outputStream.use { outputStream ->
      outputStream.write(request.toString().toByteArray())
    }

    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())

      val task = Task(
          jsonObject.getString("id"),
          jsonObject.getString("title"),
          jsonObject.getString("content"),
          jsonObject.getBoolean("isCompleted"),
          jsonObject.getInt("taskPriority")
      )

      onTaskCreated(task, null)
    }
  } catch (error: Throwable) {
    onTaskCreated(null, error)
  }

  connection.disconnect()
}).start()

Reviews

Comments