kotlin_250x250

is a superscript of Java, and the next big thing in programming, so it’s worth taking a little dabble in the language, to do something basic.

Beyond Hello , I always see if I can use it to call an API, which of course, my example below is of RegCheck.org.uk – a car registration API. You’ll need to get your own username and password at the website to try out the below code.

Create a file called .kt as below;

import java..IOException
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

fun main(vararg args: String) {
val response = sendGet(“https://www.regcheck.org.uk/api/json.aspx/Check/LA15ECT”,”**username**”,”**password**”);
println(response);
}

 

private fun sendGet(url: String, username: String, password: String ) : String {
val connection = URL(url).openConnection() as HttpURLConnection
val auth = Base64.getEncoder().encode((username + “:” + password).toByteArray()).toString(Charsets.UTF_8)
connection.addRequestProperty(“Authorization”, “Basic $auth”)
connection.connect();
val text = connection.inputStream.use { it.reader().use { reader -> reader.readText() } }
return text;
}

Compile the JAR using the code;

kotlinc http.kt -include-runtime -d http.jar

Then run it by using;

java -jar http.jar

and you should get the output

“ABICode”:”27867″,”Description”:”201 Honda Civic Type R Gt, 1996CC Petrol, 5DR, Manual”,”RegistrationYear”:”201″,”CarMake”:{“CurrentTextValue”:”Honda”},”CarModel”:{“CurrentTextValue”:”Civic”},”EngineSize”:{“CurrentTextValue”:”1996CC”},”FuelType”:{“CurrentTextValue”:”Petrol”},”MakeDescription”:”Honda”,”ModelDescription”:”Civic”,”Immobiliser”:{“CurrentTextValue”:””},”NumberOfSeats”:{“CurrentTextValue”:4},”IndicativeValue”:{“CurrentTextValue”:””},”DriverSide”:{“CurrentTextValue”:”RHD”},”Transmission”:{“CurrentTextValue”:”Manual”},”NumberOfDoors”:{“CurrentTextValue”:”″},”ImageUrl”:”http://www.regcheck.org.uk/image.aspx/@SG9uZGEgQ2l2aWM=”,”VehicleInsuranceGroup”:”21″}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here