Making API Calls Without A Library in Android
Native Android / software Engineer targeting application and software Development Opportunities with Kotlin and Java. I am passionate about exploring ways application technology can provide practical solutions to everyday problems. I’m seeking to connect with android professionals who are currently working on application and development
In Android app development, integrating with external APIs (Application Programming Interfaces) is a common requirement for fetching data, exchanging information, and enhancing app functionality. While there are several libraries available to simplify the process of making API calls, understanding the fundamental mechanisms behind these calls can be invaluable for developers seeking to optimize performance, minimize dependencies, or gain a deeper understanding of the underlying processes.
To Begin this process we need Two permissions declared in The Application Manifest File.
<uses-permission android:name = "android.permission.INTERNET"/>
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE"/>
The android.permission.INTERNET allows us to access the internet, while the android.permission.ACCESS_NETWORK_STATE would allow us to check if we are using wifi or a valid mobile internet connection .
After the manifest declaration we would check for valid internet connection by defining a class that helps achieves this.
class CheckNetwork(val connectivityManager: ConnectivityManager){
// the connectivity manager checks the state of the current network.
fun checkNetWorkState( actions:()->Unit){
if(internetConnectionIsValid())
action()
}
// function to checkncapabililities of the current network
private fun internetConnectionIsValid():Boolean{
val network = connectivityManager.activeNetwork
val capabilities = connectivityManager.getNetworkCapabilities(network)?: return false
return capaibilities.hasTransport(NetworkCapabilities.TRANSPORT__WIFI) ||
capaibilities.hasTransport(NetworkCapabilities.TRANSPORT__CELLULAR) ||
capaibilities.hasTransport(NetworkCapabilities.TRANSPORT__VPN)
}
}
we then create a class that helps us make the API calls
class Api {
val BASE_URL = BASE_URL
fun getapiCOntent(){
Thread(Runnable{
val connection = URL(BASE_URL).openConnection() as HTTpURLConnection
connection.requestMethod ="GET"
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("X-Api-Key", AppConstanta.API_KEY)
connection.connectionTimeout = 1000
connection.readTimeout = 1000
connection.doInput = 1000
try {
val reader = InputStreamReader(connection.inputStream)
reader.use { data ->
val callresponse = StringBuilder()
val bufferReader = BufferReader(data)
bufferreader.forEach{
callreaponse.append(it.trim)
connection.disconnect()
}
} catch(e:Ecxeption){
println(e.localozedmessage)
}
}).start()
}
}
class MainActivity :CompatActivity(){
private val checkNetwork by lazy {
CheckNetwork( getSystemService(ConnectivityManager::class.java)){
}
Override fun onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState){
RemoteApi().getFact()
}
}
}
Making API calls without relying on external libraries in Android involves leveraging core Android components like HttpURLConnection and AsyncTask. This approach offers developers a deeper understanding of networking processes and greater control over API interactions. By directly handling HTTP requests, parsing responses, and managing asynchronous tasks, developers can optimize performance, reduce dependencies, and gain foundational knowledge essential for advanced Android development. In this tutorial, we'll explore the step-by-step implementation of making API calls in Android applications without using external libraries, empowering developers to enhance their app's functionality with efficient and customizable network interactions.

