How to Update Data in API using Volley in Android?
Last Updated :
26 Feb, 2021
Prerequisite:
We have seen reading data from API as well as posting data to our database with the help of the API. In this article, we will take a look at updating our data in our API. We will be using the Volley library for updating our data in our API.
What we are going to build in this article?
We will be building a simple application in which we will be displaying a simple form and with that form, we will be updating our data and passing it to our API to update that data. We will be using PUT request along with volley library to update our data to API. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add the below dependency in your build.gradle file
Below is the dependency for Volley which we will be using to get the data from API. For adding this dependency navigate to the app > Gradle Scripts > build.gradle(app) and add the below dependency in the dependencies section.
// below line is used for volley library
implementation ‘com.android.volley:volley:1.1.1’
After adding this dependency sync your project and now move towards the AndroidManifest.xml part.
Step 3: Adding permissions to the internet in the AndroidManifest.xml file
Navigate to the app > AndroidManifest.xml and add the below code to it.
XML
<uses-permission android:name="android.permission.INTERNET"/>
Step 4: Working with the activity_main.xml file
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.
XML
<?xml version="1.0" encoding="utf-8"?>
<!--in this we are displaying a nested scroll view-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--edit text for our user name-->
<EditText
android:id="@+id/idEdtUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:hint="User Name" />
<!--edit text for our job-->
<EditText
android:id="@+id/idEdtJob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Job" />
<!--button to update our data-->
<Button
android:id="@+id/idBtnUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Update Data"
android:textAllCaps="false" />
<!--progress bar for the purpose of loading-->
<ProgressBar
android:id="@+id/idPBLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
<!--text view to display our
response after updating data-->
<TextView
android:id="@+id/idTVResponse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center_horizontal"
android:text="Response"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
Step 5: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
// creating our variables for our views such as
// text view, button and progress bar
// and response text view.
private EditText userNameEdt, jobEdt;
private Button updateBtn;
private ProgressBar loadingPB;
private TextView responseTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our views with their ids.
userNameEdt = findViewById(R.id.idEdtUserName);
jobEdt = findViewById(R.id.idEdtJob);
updateBtn = findViewById(R.id.idBtnUpdate);
loadingPB = findViewById(R.id.idPBLoading);
responseTV = findViewById(R.id.idTVResponse);
// adding on click listener for our button.
updateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checking if the edit text is empty or not.
if (TextUtils.isEmpty(userNameEdt.getText().toString()) && TextUtils.isEmpty(jobEdt.getText().toString())) {
// displaying a toast message if the edit text is empty.
Toast.makeText(MainActivity.this, "Please enter your data..", Toast.LENGTH_SHORT).show();
return;
}
// calling a method to update data in our API.
callPUTDataMethod(userNameEdt.getText().toString(), jobEdt.getText().toString());
}
});
}
private void callPUTDataMethod(String name, String job) {
// making our progress bar visible.
loadingPB.setVisibility(View.VISIBLE);
// url for updating our data
// in below url 2 is the identity at which
// we will be updating our data.
String url = "https://reqres.in/api/users/2";
// creating a new variable for our request queue
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
// making a string request to update our data and
// passing method as PUT. to update our data.
StringRequest request = new StringRequest(Request.Method.PUT, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// hiding our progress bar.
loadingPB.setVisibility(View.GONE);
// inside on response method we are
// setting our edit text to empty.
jobEdt.setText("");
userNameEdt.setText("");
// on below line we are displaying a toast message as data updated.
Toast.makeText(MainActivity.this, "Data Updated..", Toast.LENGTH_SHORT).show();
try {
// on below line we are extracting data from our json object
// and passing our response to our json object.
JSONObject jsonObject = new JSONObject(response);
// creating a string for our output.
String output = jsonObject.getString("name") + "\n" + jsonObject.getString("job") + "\n" + jsonObject.getString("updatedAt");
// on below line we are setting
// our string to our text view.
responseTV.setText(output);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// displaying toast message on response failure.
Toast.makeText(MainActivity.this, "Fail to update data..", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
// below line we are creating a map for storing
// our values in key and value pair.
Map<String, String> params = new HashMap<String, String>();
// on below line we are passing our key
// and value pair to our parameters.
params.put("name", name);
params.put("job", job);
// at last we are
// returning our params.
return params;
}
};
// below line is to make
// a json object request.
queue.add(request);
}
}
Now run your app and see the output of the app.
Output:
Similar Reads
How to Post Data to API using Volley in Android?
We have seen reading the data from API using Volley request with the help of GET request in Android. With the help of GET Request, we can display data from API in JSON format and use that data inside our application. In this article, we will take a look at posting our data to API using the POST requ
5 min read
Android - Update Data in API using Volley with Kotlin
Android applications use APIs to get the data from servers in android applications. With the help of APIs, we can add, read, update and delete the data from our database using APIs. We can use Volley and Retrofit for consuming data from APIs within the android application. In this article, we will t
5 min read
How to Update Data in API using Retrofit in Android?
We have seen reading data from API as well as posting data to our database with the help of the API. In this article, we will take a look at updating our data in our API. We will be using the Retrofit library for updating our data in our API. What we are going to build in this article? We will be
6 min read
Android - Update Data in API Using Volley with Jetpack Compose
APIs are used in android applications to access data from servers. We can perform various CRUD operations using these APIs within our database such as adding new data, updating data, and reading as well as updating data. In this article, we will take a look at How to Update Data in API using Volley
8 min read
How to Post Data to API using Retrofit in Android?
We have seen reading data from API in our Android app in Android Studio. For reading data from API, we use GET request to read our data which is in JSON format. In this article, we will take a look at adding data to REST API in our Android App in Android Studio. What we are going to build in this ar
6 min read
How to Post Data to API using Volley in Android using Jetpack Compose?
APIs are used within Android Applications to interact with a database to perform various CRUD operations on data within the database such as adding new data, reading the existing data, and updating and deleting existing data. In this article, we will take a look at How to Post data to API in android
3 min read
How to Update Data in Realm Database in Android?
In previous articles, we have seen adding and reading data from our realm database in Android. In that article, we were adding course details in our database and reading the data in the form of a list. In this article, we will take a look at updating this data in our android app. What we are going
7 min read
How to Update Data in Back4App Database in Android?
In the previous article, we have seen adding as well as reading data from our Bac4App database in the Android app. In this article, we will take a look at Updating this data in your database. What we are going to build in this article? We will be building a simple application in which we will be u
7 min read
How to Update Data in API using Retrofit in Android using Jetpack Compose?
Android applications use APIs within Android Applications to access data from databases. We can perform several operations using APIs such as Reading, Writing, and Updating our Data in the Database. In this article, we will take a look at How to Update Data in API using Retrofit in Android using Jet
9 min read
Making API Calls using Volley Library in Android
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
4 min read