Git Product home page Git Product logo

android_volley_basics's Introduction

Android_Volley_Basics

Using Volley Library to fetch data from the API

This topic is a part of My Complete Andorid Course

Dependency

implementation 'com.android.volley:volley:1.2.1'
  • If String Request
implementation 'com.google.code.gson:gson:2.8.9'

Code

1st Activity

TextView textView;
Button button;
LinearLayout linearLayout;

RequestQueue requestQueue;

        //whenever you want to fetch data from the API you create a request queue
        requestQueue = Volley.newRequestQueue(getApplicationContext());
        
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //url of API
                String url = "https://mocki.io/v1/db6249d4-0ee1-4bae-a948-82d92c7d1ebc";

                //if you see it is first object then array so...
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {

                            //we want array (from the object) so...
                            JSONArray jsonArray = response.getJSONArray("student");

                            //now we have 3 items in array students[] (just we got)
                            for (int i = 0; i < jsonArray.length(); i++) {

                                //we are having objects (name, email, age) so...
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                                String name = jsonObject.getString("name");
                                String email = jsonObject.getString("email");
                                int age = jsonObject.getInt("age");

                                //Just Appending the data to already existing TextView
                                textView.append(name + " " + email + " " + String.valueOf(age) + "\n");

                                //Creating TextView and Adding in the Layout
                                TextView textView2 = new TextView(MainActivity.this);
                                textView2.setText(name + " " + email + " " + String.valueOf(age) + "\n");
                                linearLayout.addView(textView2);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

                //add this request to request queue
                requestQueue.add(jsonObjectRequest);
            }
        });
    }
}

App Highlight

Volley API Basic App Volley API Basic Code

android_volley_basics's People

Contributors

ananddasani avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.