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.
The code snippets that are references should be listed below, after this readme file.
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:
compile 'com.mcxiaoke.volley:library:1.0.+@aar'
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.
To retrieve a JSON Object, see the code example.
- The code is very similar for JSON arrays
- The
TAG
variable is just used for Logcat - the response is being made into a string and output into the Logcat console. onReponse
is where you would do what you need with the returned data
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.