Threads and asynchronous tasks have never been pleasant in Android. It is a nightmare to handle screen orientation changes or update the UI from a common thread. However, the guys at Google came up with Volley and it is an excellent way to make Android HTTP requests.
Out of the box you have the ability to make a request that will return a JSONArray or JSONObject which is pretty slick because most APIs use those as a standard.
First start by cloning the Volley library from the Google source tree:
git clone https://android.googlesource.com/platform/frameworks/volley
If you’re like me, you’ll be using the command line and a text editor rather than an IDE like Eclipse. Using the command line, navigate to this newly cloned Volley Git repository and execute the following command:
ant jar
This will create a standalone JAR archive of the Volley library project which you will then put into the libs directory of the project that will actually be using Volley.
Let’s continue to use the command line and create our new Android project:
android create project --target 14 --name VolleyTest --path ./VolleyTest --activity VolleyTestActivity --package com.nraboy.volleytest
Android Volley uses network and internet, so it is very important to add the correct permissions to your AndroidManifest.xml, otherwise you will either get errors or no results at all.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
The following code should go into your VolleyTestActivity.java file. You’ll notice that in this example we are making a request that expects a JSONArray
response. The JsonArrayRequest
constructor takes three parameters, the first of which is a URL and the other two being callbacks. Since Volley is asynchronous, you cannot expect your application to suspend until the network request is complete. Let Volley make use of the response and error callbacks when the request has completed.
package com.nraboy.volleytest;
import android.content.Context;
import android.util.Log;
import com.android.volley.*;
import com.android.volley.toolbox.*;
public class VolleyTestActivity extends Activity {
private Context context;
@Override
public void onCreate(savedInstanceState) {
super.onCreate(savedInstanceState);
this.context = this;
makeRequest("http://www.example.com/api/test?key=1234");
}
private void makeRequest(String url) {
RequestQueue queue = Volley.newRequestQueue(this.context);
JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jObject = null;
for(int = 0; i < response.length(); i++) {
try {
jObject = response.getJSONObject(i);
Log.d("VolleyTest", jObject.getString("name"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("VolleyTest", error.getMessage());
}
});
}
}
In the above example we are making a request to a fictional URL at http://www.example.com/api/test?key=1234 where key=1234
is one of our GET parameters. If you were to try this fictional URL, the ErrorListener would be triggered because the request would fail. However, a successful Android HTTP request would trigger our success listener and then we would make effort to work with the JSONArray
returned. In this example, the JSONArray
is composed of JSONObject
, so we loop through the array and print out properties from each of the objects.