Using Spring Android --------------------- - Using Spring in Android applications usually involves and Android application using Spring to work with RESTful resources. - There's an article about the topic at: http://spring.io/guides/gs/consuming-rest-android Spring RESTful clients ---------------------- - Spring itself (not just for Android) provides a RestTemplate type that simplifies acting as a RESTful client. - The RestTemplate type defines methods to perform the classic REST operations, including: // GETs a Java object from a RESTful URL T getForObject(String url, Class objectType) // POSTs a Java object to a RESTful URL and // returns the Location header (the new object's URL) URI postForLocation(String url, Object obj) // PUTs a Java object to a RESTful URL void put (String url, Object obj) // delete a RESTful representation void delete (String url); - The template methods that work with objects require at least one MessageConverter that know how to convert a RESTful representation (e.g. JSON) to/from Java objects. For JSON, you can use MappingJackson2HttpMessageConverter. For XML, Jaxb2RootElementHttpMessageConverter. After creating the RestTemplate, you can add message converter(s): RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add( new MappingJackson2HttpMessageConverter()); - After configuring the MessageConverter, you can work with RESTful resources: MyClass g = restTemplate.getForObject(url, MyClass.class); Configuring an Android Project For Spring-Android ------------------------------------------------- - If you are using the Android Development Kit, you can create a normal Android project, and then add the Spring-Android JARs to the project's "libs" folder. At minimum, for RestTemplate and JSON, you need these JARs: jackson-annotations-x.x.x.jar // x.x.x is version jackson-core-x.x.x.jar jackson-databind-x.x.x.jar spring-android-auth-y.y.y.RELEASE.jar spring-android-core-y.y.y.RELEASE.jar spring-android-rest-template-y.y.y.RELEASE.jar Coding an Android Rest Application ----------------------------------- - Then you can create an Android layout and Activity in the normal way. Then determine what will trigger your app's REST requests - for example, you might use a button click: myButton = (Button) findViewById(R.id.button1); myButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // code to initiate REST request goes here new MyAsyncTask().execute(); // see next } }); - For best responsiveness, you should use an asynchronous technique to make the request. Android provides the AsyncTask type that you can subclass and in which you can code your methods to work with RESTful resources. private class MyAsyncTask extends AsyncTask { . . . } The three parameterized types to AsyncTask are parameters, progress and result - in this example, we will only work with the result. We type this generic class to MyClass so the method in our subclass can work with MyClass objects. - The AsyncTask actually runs through four steps, but we will only use two - doInBackground(), in which we can initiate our asynchronous request and onPostExecute() which Android calls when the request is complete. Here's what the code to get a RESTful resource might look like (exception handling omitted): private class MyAsyncTask extends AsyncTask { // start the async request protected MyClass doInBackground(Void... params) { final String url = ... // URL of JSON RESTful resouce RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); MyClass g = restTemplate.getForObject(url, MyClass.class); return g; } // display results of request protected void onPostExecute(MyClass g) { Toast.makeText(MainActivity.this, g.toString(), Toast.LENGTH_SHORT) .show(); } }