Android Tutorial Day 6

Learning Objective: Let’s learn how to call online API and get data from the server to our Android App. We will explore it using volley.

To add Volley Library to your Android Project, Add the following code to Gradle. Reference

dependencies {
    implementation ''

Now I would like to check out some live JSON API with sample data and I found the following URL:

Now Let’s add Volley code to fetch data from above URL

    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {


        binding = FragmentFirstBinding.inflate(inflater, container, false);
        return binding.getRoot();


    void callAPI()
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(getContext());
        String url = "";

        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    public void onResponse(String response) {


                        try {

                            JSONObject obj = new JSONObject(response.toString());

                            Log.e("cs", obj.getString("userId"));
                            Log.e("cs", obj.getString("id"));
                            Log.e("cs", obj.getString("title"));
                            Log.e("cs", obj.getString("completed"));

                        } catch (Throwable t) {
                            Log.e("cs", "Could not parse malformed JSON: \"" + "" + "\"");

                }, new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {

        // Add the request to the RequestQueue.


output you will get it on Log Cat. Dont forget to add internet permission into your Android Manifests

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