![]() |
VOOZH | about |
Volley is an HTTP library that’s used for caching and making a network request in Android applications. It is an HTTP library that makes networking for Android apps easier and most importantly, faster. API stands for Application Programming Interface. It is a way for two or more computer programs to communicate with each other. By using its products or services communicate with other products and services without having to know how they’re implemented.
Note: This Android article covered in both Java and Kotlin languages.
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Add internet permission to your app
Go to app > manifest.xml file and add the internet permission. Below is the code for the manifest file.
Step 3: Add the Volley dependency to build.gradle (Module : app ) file
Go to app > Gradle Scripts > build.gradle (Module : app) file and add the dependency. Below is the code for the build.gradle file.
plugins {
id 'com.android.application'
}android {
compileSdk 31defaultConfig {
applicationId "com.example.gfgvolleyapicall"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'implementation 'com.android.volley:volley:1.2.1' // adding volley dependency
}
Step 4: Working with activity_main.xml
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 5: Working with the MainActivity File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
Output: