The Volley library was developed by Google and first introduced during Google I/O in 2013. This library was developed because there is an absence in Android SDK, that can carry networking without interfering with the user experience. It is an HTTP library and it actually makes networking very easy and fast for Android applications. This library automatically schedules all network requests, such as fetching responses for images from the web. It provides transparent disk and memory caching.
This library has a cancellation request API which is a powerful request for canceling out some blocks of requests or for canceling a single request.
It provides powerful customization abilities.
It provides debugging and tracing tools.
Automatically schedules all network requests.
Multiple concurrent network connections.
Built-in support for string, images, JSON object, and JSON array requests.
It supports retrying requests.
Volley is not suitable for streaming and large downloads.
Volley does not have proper documentation yet.
Volley is slower as compared to Retrofit.
Volley has a complex code structure.
Volley has two classes:
This class is used to dispatch network requests. It is the basic building block of the volley. With the help of this, we can manage the worker threads such that we can handle parallel requests apart from those which are responsible for reading and writing from and to the caches. It works in a First In First Out (FIFO) manner.
It contains the necessary information to make API requests. This class is used to support POST and GET requests. This class acts as the base class which can be extended to define a custom request.
Volley provides the following types of requests built-in:
String Request: It is used when we want the response returned in the form of a string. We can then parse the response using JSON or GSON as per our requirement.
JSONObject Request: This request is used to send and receive JSONObject from the server.
JSONArray Request: This request is used to send and retrieve JSONArray to and from the server. It cannot handle a request that returns a JSONObject.
Image Request: To fetch images from any URL we can use imageRequest which is the usual way to do this. It returns a Bitmap Object that we can eventually display in our ImageView.
Example:
Step 1: First we create a new Android project in Android studio.
Step 2: Dependencies:
In our project, we have to add the following dependencies in the build.gradle file.
1 2
implementation 'com.android.support:design:26.1.0' implementation 'com.android.volley:volley:1.1.0'
Step 3: Permission:
We need to add permission in AndroidMinfest.xml
.
1
<uses-permission android:name="android.permission.INTERNET"/>
Step 4: activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<RelativeLayout android:layout_width=”match_parent” android:layout_height=”match_parent” tools:context=”MainActivity”> <TextView android:id=”@+id/textDisplay” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Hello world” android:layout_conterHorizontal=”true” /> <ProgressBar android:id=”@+id/progressBar” style=”?android:attr/progressBarStyleLarge” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_conterInParent=”true” /> </RelativeLayou>
Step 5: 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
58
package example.volley.com.androidvolleyexample;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extends AppCompatActivity {
private TextView txtShowResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txthowResult = findViewById(R.id.txtDisplay);
RequestQueue requestQueue = Volley.newRequestQueue(this);
final String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&keyword=cruise&key=[YOUR-GOOGLE-SEARCH-API-KEY]";
JsonObjectRequest jObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
try {
StringBuilder formattedResult = new StringBuilder();
JSONArray responseJSONArray = response.getJSONArray("results");
for (int i = 0; i < responseJSONArray.length(); i++) {
formattedResult.append("\n" + responseJSONArray.getJSONObject(i)
.get("name") + "=> \t" + responseJSONArray.getJSONObject(i)
.get("rating"));
}
txtShowResult.setText("List of Restaurants \n" + " Name" + "\t Rating \n" + formattedResult);
} catch (JSONException e) {
e.printStackTrace();
}
findViewById(R.id.progressBar)
.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
txtShowResult.setText("An Error occurred while making the request");
}
});
requestQueue.add(jObjReq);
}
}
Output: