Volley, Android Studio and Examples
This tutorial is really just some code snippets to show you how to use Volley in Android Studio for your Android project. The tutorial will show you how you can import Volley, an example of retrieving a JSON object and retrieving an image for use in a layout.
Files and Code Snippets
The code snippets that are references should be listed below, after this readme file.
Importing Volley into Android Studio
To import Volley into your project, you have to use a 3rd party mirror or the Volley library. However, it is only 1 line of code. Open your app/build.gradle and add the following dependency:
The first class you will need is a singleton that handles Volley requests. See the AppController code snippet.
Second, you will need an image cache, see the LruBitmapCache code snippet.
Getting a JSON Object or JSON Array
To retrieve a JSON Object, see the code example.
- The code is very similar for JSON arrays
TAGvariable is just used for Logcat - the response is being made into a string and output into the Logcat console.
onReponseis where you would do what you need with the returned data
Displaying an Image in NetworkImageView
To display an image, in your layout, you will need a NetworkImageView. This is similar to an ImageView. See the NetworkImageView XML example.
The code to display an image in the NetworkImageView is:
NetworkImageView imageView = (NetworkImageView) findViewById(R.id.image); imageView.setImageUrl("http://www.website.com/path/to/image.jpg", AppController.getInstance().getImageLoader());
If you would like to set placeholder and error images, see the Placeholder and Error Images code sample.