A – What is Volley?
Volley is a very powerful and easy to use library for Android that can be used to handle networking connection.
It manages the processing and caching of network requests and it saves developers valuable time from writing the same network call/cache code again and again.
Volley comes with lot of features. Some of them are:
- Automatically schedule all network requests
- Provides transparent disk and memory caching
- Provides powerful cancellation request API
- Provides powerful customization abilities
- Provides debugging and tracing tools
B – Set up Volley
Firstly, we need to clone the Volley project from the address:
git clone https://android.googlesource.com/platform/frameworks/volley
Then import the downloaded source into your app project as an Android library project or make a .jar file and add to project build path.
There are 2 main classes in Volley library:
- RequestQueue: handles all the requests, takes care of queuing the requests and handle the responses
- Request: contains all the necessary details for making web API call. For example: which method to Use (GET or POST), request data to pass, response listener, error listener
C - Android Volley – Make Simple Request
1. Add the INTERNET permission
Update AndroidManifest.xml file by adding INTERNET permission. As you know, without this permission your app won’t be able to connect to the network.
<uses-permission android:name="android.permission.INTERNET"/>
2. Create an object of RequestQueue class
RequestQueue requestQueue = Volley.newRequestQueue(this);
3. Create a simple request
In this tutorial, I’ll use Volley to send a simple String request to receive response from provided URL.
StringRequest stringRequest; String url = "http://icetea09.com"; stringRequest = new StringRequest(Request.Method.GET, url, new Listener<String>() { @Override public void onResponse(String res) { tvResponse.setText(res); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError err) { tvResponse.setText(err.getMessage()); } });
4. Add request into the RequestQueue
requestQueue.add(stringRequest);
D – Complete source code of Android Volley – Make Simple Request
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="ice.tea.volleysimplerequest.MainActivity" > <Button android:id="@+id/btn_send_request" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Request" /> <TextView android:id="@+id/txt_response" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignLeft="@+id/btn_send_request" android:layout_below="@+id/btn_send_request" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends Activity { TextView tvResponse; Button btnRequest; RequestQueue requestQueue; StringRequest stringRequest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResponse = (TextView)findViewById(R.id.txt_response); btnRequest = (Button)findViewById(R.id.btn_send_request); requestQueue = Volley.newRequestQueue(this); String url = "http://icetea09.com"; stringRequest = new StringRequest(Request.Method.GET, url, new Listener<String>() { @Override public void onResponse(String res) { tvResponse.setText(res); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError err) { tvResponse.setText(err.getMessage()); } }); btnRequest.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { requestQueue.add(stringRequest); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Demo result:
E – Source Code
http://www.mediafire.com/?2ok5tjh2247sc2a
https://docs.google.com/file/d/0Bw3dwdSezn6fUmdiazFZd2NjNms/edit?usp=docslist_api