This Getting Started guide walks you through the process of building an application that uses Spring for Android’s RestTemplate
to consume a REST service.
You’ll fetch some publicly visible Facebook data using their REST API from Android.
pom.xml
link:initial/pom.xml[role=include]
AndroidManifest.xml
link:initial/AndroidManifest.xml[role=include]
Add a text string. Text strings can be referenced from the application or from other resource files.
res/values/strings.xml
link:initial/res/values/strings.xml[role=include]
Here you define the visual structure for the user interface of your application.
res/layout/hello_layout.xml
link:initial/res/layout/hello_layout.xml[role=include]
Before you create the REST request, consider the data that you want your application to consume. Suppose that you want to find out what Facebook knows about Pivotal. Knowing that Pivotal has a page on Facebook and that the ID is "gopivotal", query Facebook’s Graph API via this URL:
http://graph.facebook.com/gopivotal
If you request that URL through your web browser or curl, you’ll receive a JSON document that looks something like this:
{
"id": "161112704050757",
"about": "At Pivotal, our mission is to enable customers to build a new class of applications, leveraging big and fast data, and do all of this with the power of cloud independence. ",
"app_id": "0",
"can_post": false,
"category": "Internet/software",
"checkins": 0,
"cover": {
"cover_id": 163344023827625,
"source": "http://sphotos-d.ak.fbcdn.net/hphotos-ak-frc1/s720x720/554668_163344023827625_839302172_n.png",
"offset_y": 0,
"offset_x": 0
},
"founded": "2013",
"has_added_app": false,
"is_community_page": false,
"is_published": true,
"likes": 126,
"link": "https://www.facebook.com/gopivotal",
"location": {
"street": "1900 South Norfolk St.",
"city": "San Mateo",
"state": "CA",
"country": "United States",
"zip": "94403",
"latitude": 37.552261,
"longitude": -122.292152
},
"name": "Pivotal",
"phone": "650-286-8012",
"talking_about_count": 15,
"username": "gopivotal",
"website": "http://www.gopivotal.com",
"were_here_count": 0
}
As you can see, Facebook returns quite a bit of information. This guide deals with a small part of it.
To model this JSON data, you create a representation class that defines a few of these fields. The following example uses Jackson annotations. Jackson is a powerful JSON processor for Java that you can use within Spring.
src/main/java/org/hello/Page.java
link:complete/src/main/java/org/hello/Page.java[role=include]
As you can see, this is a simple Java class with a handful of properties and matching getter methods. It’s annotated with @JsonIgnoreProperties
from the Jackson JSON processing library to indicate that any properties not bound in this type should be ignored.
Spring provides a template class called RestTemplate
. RestTemplate
makes interacting with most RESTful services a simple process. In the example below, you establish a few variables and then make a request of our simple REST service. As mentioned earlier, you use Jackson to marshal the JSON response data into our representation classes.
src/main/java/org/hello/HelloActivity.java
link:complete/src/main/java/org/hello/HelloActivity.java[role=include]
So far, you have only used the HTTP verb GET
to make calls, but you could just as easily have used POST
, PUT
, and so on.