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.

Create a HTTP Connection

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

Learn how to create an HTTP connection, and how to send and receive data using streams. Add timeouts and headers, receive data as JSON.

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 creating the NetworkStatusChecker, you can implement and API call or request, to register a user!

private val networkStatusChecker by lazy {
  NetworkStatusChecker(getSystemService(ConnectivityManager::class.java))
}
networkStatusChecker.performIfConnectedToInternet {
 ...
}
Thread(Runnable {

}).start()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/register").openConnection() as HttpURLConnection
  connection.requestMethod = "POST"
  connection.setRequestProperty("Content-Type", "application/json")
  connection.setRequestProperty("Accept", "application/json")
}).start()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/register").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
}).start()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/register").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 = "{\"name\":\"${userDataRequest.name}\", \"email\":\"${userDataRequest.email}\", " +
      "\"password\":\"${userDataRequest.password}\"}"

  val bytes = body.toByteArray()
}).start()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/register").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 = "{\"name\":\"${userDataRequest.name}\", \"email\":\"${userDataRequest.email}\", " +
      "\"password\":\"${userDataRequest.password}\"}"

  val bytes = body.toByteArray()

  try {
    connection.outputStream.use { outputStream ->
      outputStream.write(bytes)
    }
  } catch (error: Throwable) {
    
  }
}).start()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/register").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 = "{\"name\":\"${userDataRequest.name}\", \"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) {
    
  }
}).start()
Thread(Runnable {
  val connection = URL("$BASE_URL/api/register").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 = "{\"name\":\"${userDataRequest.name}\", \"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())
        }
      }

      onUserCreated(response.toString(), null)
    }
  } catch (error: Throwable) {
    onUserCreated(null, error)
  }

  connection.disconnect()
}).start()
runOnUiThread {
...
}

Reviews

Comments