HTTP请求在Kotlin

我对Kotlin是全新的。 我想要使​​用POST方法进行登录验证,并使用GET方法获取一些信息。 我的URL,服务器用户名和密码已经是我以前的项目。 我没有找到使用这个东西的任何适当的示例项目。 任何人都请建议我任何工作的例子,我可以在HTTP请求中使用GET和POST方法

您可以使用在Java中使用的标准库。 例如HttpURLConnection

 private fun sendGet() { val url = "http://www.google.com/" val obj = URL(url) with(obj.openConnection() as HttpURLConnection) { // optional default is GET requestMethod = "GET" println("\nSending 'GET' request to URL : $url") println("Response Code : $responseCode") BufferedReader(InputStreamReader(inputStream)).use { val response = StringBuffer() var inputLine = it.readLine() while (inputLine != null) { inputLine = it.readLine() response.append(inputLine) } println(response.toString()) } } } 

这就是你可以发送简单的HTTP请求到服务器。 我会建议Apache HttpComponents或任何你想用于复杂任务的框架。 对于Android Volley是开始的地方。