Diya Tech Lab Blog

22
Sep - 2017

Fetch Json Data Using Volley

saloniparikh

hey guys, welcome to Diya tech lab. In this  post we are going to learn about how to fetch json data using Volley.I have a Url that gives me the JSON data which we will parse using Volley.so if you really want to learn and earn some knowledge about Json – Volley then move ahead  and see this Tutorial.

Before getting started  I  assume that u have

  • Android studio installed in your system

now, let’s start with the App We are going to make

Create new Project

For using Volley , you need to add Volley in your Project

see the screenshot  and add Volley in Module app inside Gradle Scripts

In this Add the dependencies  given below:

compile ‘com.android.volley:volley:1.0.0’

Sync your project.

Open AndroidManifest.java and write permission

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

For fetching the data and storing it in ListView you need

  • Main Activity Class(MainActivity.java & activity_main.xml)
  • Data Model Class(Demo.java)
  • to store fetched data in objects
  • Custom Adapter Class(ListViewAdapter.java)
  • for creating custom ListView
  • Custom Layout File for displaying list of Items(list_item.xml)
  • to display the item you have in your Json file and you want to display in your listview.

Url that contains Json Data:

                “http://sales.arkayapps.com/playquiz/getleveliinfo.php”
MainActivity.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class MainActivity extends AppCompatActivity {

    private static final String JSON_URL = "http://sales.arkayapps.com/playquiz/getleveliinfo.php";

    //listview object
    ListView listView;

    List<Demo> DemoList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.list1);

        DemoList = new ArrayList<>();

        levelinfolist();
    }

    private void levelinfolist() {

        StringRequest stringrequest = new StringRequest(Request.Method.GET, JSON_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray jarray = new JSONArray(response);
                            for (int i = 0; i < jarray.length(); i++) {
                                JSONObject demoObject = jarray.getJSONObject(i);

                                Demo demo = new Demo(demoObject.getString("lelveno"), demoObject.getString("levelname"));


                                DemoList.add(demo);
                            }
                            ListViewAdapter adapter = new ListViewAdapter(DemoList, getApplicationContext());

                            //adding the adapter to listview
                            listView.setAdapter(adapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
                    RequestQueue requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(stringrequest);
    }
}

activity_main.xml


1
2
3
4
<ListView
    android:id="@+id/list1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>

Demo.java (Model Class)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Demo {
String lelveno, levelname;

public Demo(String lelveno, String levelname) {
this.lelveno = lelveno;
this.levelname = levelname;
}

public String getlelveno() {
return lelveno;
}

public String getlevelname() {
return levelname;
}
}

String name and key name of json file must be the same

for example:

lelveno and levelname same as u can see in the image of Url

ListViewAdapter.java (Custom Adapter)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class ListViewAdapter extends ArrayAdapter<Demo> {
    private List<Demo> demoList;


    private Context mCtx;

    //so while creating the object of this adapter class we need to give demolist and context
    public ListViewAdapter(List<Demo> demoList, Context mCtx) {
        super(mCtx, R.layout.list_items, demoList);
        this.demoList = demoList;
        this.mCtx = mCtx;
    }

//
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);

        //creating a view with our xml layout
        View listViewItem = inflater.inflate(R.layout.list_items, null, true);

        //getting text views
        TextView txtno = (TextView) listViewItem.findViewById(R.id.no);
        TextView txtname =(TextView) listViewItem.findViewById(R.id.name);

        //Getting the hero for the specified position
        Demo demo = demoList.get(position);

        //setting hero values to textviews
        txtno.setText(demo.getlelveno());
        txtname.setText(demo.getlevelname());

        //returning the listitem
        return listViewItem;
    }

}

list_items.java (custom layout file)


1
2
3
4
5
6
7
8
9
10
11
12
<TextView
   android:id="@+id/no"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

<TextView
   android:id="@+id/name"
   android:layout_marginTop="5dp"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textColor="#08308e" />

Now we are ready to run our Application.

it will look like this.This will show your Url data in Listview using Json-Volley.I hope  this tutorial is helpful for you to make an Application and you are now clear with the concept.please share your reviews and experience via comments.if you still have any queries, fill free to ask.