Volley Library Fetching JSON Data from URLIn this tutorial, we will fetch the JSON data from the URL using Volley library. Volley is an HTTP Library which provides the facilities for the network connectivity of our app. The advantages of using the Volley library are as follows:
Android Volley Fetching JSON Data from URL ExampleIn this example, we will load the JSON data from the URL using Volley library. The JSON data contains the String "name", String "imageurl" and String "description" of tutorials. After fetching the data from the URL, they are displayed in ListView. You can refer more about JSON parsing tutorial at https://www.javatpoint.com/android-json-parsing-tutorial. Let's create a JSON data which contains the following information. JSON data at URL (provide your URL of information) = http://192.168.1.35:8080/jsondata/ The JSON data looks like as: Create an activity_main.xml in layout and add the following code. activity_main.xmlNow, create a list_item.xml file in the layout directory which contains the row items for ListView. This contains one ImageView for image display and two TextView for text display. list_item.xmlCreate a data model class with name Tutorial.java and information String "name", String "imageurl" and String "description". Tutorial.javaCreate a custom adapter class named as MyAdapter.java and extend ArrayAdapter to handle the custom ListView. In this class we are calling an anonymous class new ImageDownloaderTask(holder.imageView).execute(imageUrl) to download images from URL. MyAdapter.javaAdd the following library files in the build.gradle file. build.gradleCreating an anonymous class ImageDownloaderTask.java which extends AsyncTask<>. This class downloads (or fetch) the images from URL while executing doInbackground() method and returns the result (bitmap) to onPostExecute(). In onPostExecute() method, the bitmap are set to the ImageView. ImageDownloaderTask.javaIn MainActivity.java class, we are fetching and parsing the JSON data from the URL using Volley. MainActivity.javaAdd the Internet permission in AndroidManifest.xml AndroidManifest.xmlOutput:
Next TopicAndroid Linkify
|