Volley Tutorial:How to fetch data from the URL in android using volley library.

1.What is volley ?

Volley is an HTTP library which is used to make networking calls for the android applications easier and faster.

It is also available GitHub.

2.Advantage of Volley.

a>  Add multiple connection

b> Queuing and prioritization the request

c>Cancelling the API

d>Easy to customize

Now understand with the help of Example:

Note:

Open the AndroidManifest.xml and put the below code:

<uses-permission android:name="android.permission.INTERNET"/>

step 1:

Suppose you have given the JSON data in the Student_URL=”https://placementpreparation/record/student”;

{
"student":
[ 
{
"name":"suman kumar","age":"24"
},
{
"name":"rahul kumar","age":"22"
},
{
"name":"raj kumar","age":"23
},
{
"name":"amit kumar","age":"20"
}
]
}

Step 2:

Open your build.gradle and put code

dependencies
{
implementation 'com.android.volley:volley:'1.0.0'
}

step 3:

Open the res/layout/student_detail.xml and put the below code:

This layout is used to show the name and age of the student in the listview.

student_detail.xml

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="Vertical"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/student_name"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/student_age"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

Step 4:

Open the res/layout/student_list.xml and put the below the code:

This layout is for the student list and in which we can show the different number of student.

student_list.xml

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<ListView
android:id="@+id/student_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

Step 5:

Student.java

Make the Response class in which we can get the data from the URL.

public class Student
{
private String name;
private String age;

public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age=age;
}
}

Step 6:

Now we will make the Singleton class for the HTTP request in which we can fetch the data from the URL.

VolleySingleton.java

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.context.Context;
import com.android.volley.toolbox.Volley;
public class VolleySingleton 
{
private static VolleySingleton instance;
private RequestQueue queue;
private static Context mcontext;

private VolleySingleton(Context context)
{
mcontext=context;
queue=getRequestQueue();
}
public static synchronized Volleysingleton getInstance(Context context) 
{
if(instance==null)
{
instance=new VolleySingleton(context);
}
return instance;
}
public RequestQueue getRequestQueue()
{
if(queue==null)
{
queue=Volley.newRequestQueue(mcontext.getApplicationContext());
}
return queue;
}
//adding the request to the request queue
 public <T> void addToRequestQueue(Request<T> request) {
        getRequestQueue().add(request);
    }

}

Step 7:

In this activity we will call the API and Parsing the JSON data and then set on the listview.

Mainactivity.java

import android.app.Activity;
import android.example.com.VolleySingleton;
public class Mainactivity extends Activity
{
private ListView studentList;
private VolleySingleton singleton;//Here calling the volley class
private CustomAdapter customadpter; 
private ArrayList<Student> valueInTheAdapter;
@Override
public void onCreate(Bumdle SavedInstanceState)
{
super.onCreate(SavedInstanceState);
setContentView(R.layout.student_list);
singleton=VolleySingleton.getInstance(getApplicationContext());
studentList=(ListView)findViewById(R.id.student_list);
studentListApi();
}
private void studentListApi()
{
JsonObjectRequest jsonObject=new JsonObjectRequest(Request.Method.GET,Student_URL,Response.Listner<JsonObject>()
{
@Override
public void OnResponse(JsonObject response)
{
//Here we will get the data from the URL and this data will set in the adapter.
try
{
valueInTheAdapter=new ArrayList<>();

if(response.lenght>0)
{
JSONArray array=response.getJSONArray("student");
for(int i=0;i<array.length;i++)
{
JSONObject object=array.get(i);
Student student_object=new Student();
student_object.setName(object.getString("name"));
student_object.setAge(object.getString("age"));
valueInTheAdapter.add(student_object);
}
customadpter=new CustomAdapter(getApplicationContext,valueInTheAdapter);
studentList.setadapter(customadpter);
}
}

}
catch(Exception e)
{

}

Log.d("gettingResponse" ,""+response);

}
,new Response.ErrorListner()
@Override
public void onErrorResponse(error)
{
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
               
 return null;
}
};
singleton.addToRequestQueue(jsonObject);
}




private class CustomAdpter extends BaseAdapter
{
Context context;
List<Student> student;
private CustomAdapter(Context context,List<Student> student)
{
this.context=context;
this.student=student;
}
@Override
public int getCount()
{
return student.size();
}
@Override
public object getItem()
{
return student.get(position);
}
@Override
public long getItemId()
{
return student.indexOf(student.get(position));
}
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
if(convertView==null)
{
LayoutInflater inflater=LayoutInflater.from(getContext());
convertView=inflater.inflate(R.layout.student_detail,false);
TextView name=(TextView)convertView.findViewById(R.id.student_name);
TextView age=(TextView)convertView.findViewById(R.id.student_age);

name.setText(student.get(position).getName());
age.settext(student.get(position).getAge());
}
return convertView;
}
}
}

Step 8:

Now Here we will get the list of student.

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *