Back to project page SpringForAndroidDemo.
The source code is released under:
Apache License
If you think the Android project SpringForAndroidDemo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.sample.springforandroiddemo; //from w w w. j av a2s .c o m import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends ActionBarActivity { // --------------------------------------------------------------------------- // Class Constants private static final String KEY_GREETING = "GREETING"; // --------------------------------------------------------------------------- // Member Variables private Greeting greeting; private TextView greetingIdText ; private TextView greetingContentText ; // --------------------------------------------------------------------------- // Class Overrides @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Reference the UI Elements greetingIdText = (TextView) findViewById(R.id.id_value); greetingContentText = (TextView) findViewById(R.id.content_value); // If savedInstanceState is not null if(savedInstanceState != null){ // Get the Greeting from the Bundle greeting = savedInstanceState.getParcelable(KEY_GREETING); // Update the UI updateView(greeting); } } @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_refresh) { // Make a new Request to get the Greeting new HttpRequestTask().execute(); return true; } return super.onOptionsItemSelected(item); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable(KEY_GREETING, greeting); } // --------------------------------------------------------------------------- // Public / Private Helpers /** * Updates the View Elements * @param greeting - the greeting retrieved from refreshing the URL */ private void updateView(Greeting greeting){ // If the greeting is not null if(greeting != null){ // Update the UI greetingIdText.setText(greeting.getId()); greetingContentText.setText(greeting.getContent()); } } // --------------------------------------------------------------------------- // Background Tasks private class HttpRequestTask extends AsyncTask<Void, Void, Greeting> { @Override protected Greeting doInBackground(Void... params) { try { final String url = "http://rest-service.guides.spring.io/greeting"; RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); greeting = restTemplate.getForObject(url, Greeting.class); return greeting; } catch (Exception e) { Log.e("MainActivity", e.getMessage(), e); } return null; } @Override protected void onPostExecute(Greeting greeting) { // Update the view updateView(greeting); } } }