If you think the Android project spring-android-samples listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2010-2014 the original author or authors.
*/*fromwww.java2s.com*/
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package org.springframework.android.showcase.rest;
import java.util.ArrayList;
import java.util.List;
import org.springframework.android.showcase.AbstractAsyncActivity;
import org.springframework.android.showcase.R;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.SimpleXmlHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* @author Roy Clarkson
* @author Helena Edelson
* @author Pierre-Yves Ricau
*/publicclass HttpGetParametersActivity extends AbstractAsyncActivity {
protectedstaticfinal String TAG = HttpGetParametersActivity.class.getSimpleName();
// ***************************************
// Activity methods
// ***************************************
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.http_get_parameters_activity_layout);
// Initiate the request for JSON data when the JSON button is pushed
final Button buttonJson = (Button) findViewById(R.id.button_json);
buttonJson.setOnClickListener(new View.OnClickListener() {
publicvoid onClick(View v) {
new DownloadStateTask().execute(MediaType.APPLICATION_JSON);
}
});
// Initiate the request for XML data when the XML button is pushed
final Button buttonXml = (Button) findViewById(R.id.button_xml);
buttonXml.setOnClickListener(new View.OnClickListener() {
publicvoid onClick(View v) {
new DownloadStateTask().execute(MediaType.APPLICATION_XML);
}
});
}
// ***************************************
// Private methods
// ***************************************
privatevoid showState(State state) {
// display a notification to the user with the state
if (state != null) {
Toast.makeText(this, state.getFormattedName(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No state found with that abbreviation!", Toast.LENGTH_LONG).show();
}
}
// ***************************************
// Private classes
// ***************************************
privateclass DownloadStateTask extends AsyncTask<MediaType, Void, State> {
private String abbreviation;
@Override
protectedvoid onPreExecute() {
showLoadingProgressDialog();
// retrieve the abbreviation from the EditText field
EditText editText = (EditText) findViewById(R.id.edit_text_abbreviation);
abbreviation = editText.getText().toString();
}
@Override
protected State doInBackground(MediaType... params) {
try {
if (params.length <= 0) {
return null;
}
MediaType mediaType = params[0];
// The URL for making the GET request
final String url = getString(R.string.base_uri) + "/state/{abbreviation}";
// Set the Accept header for "application/json" or "application/xml"
HttpHeaders requestHeaders = new HttpHeaders();
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(mediaType);
requestHeaders.setAccept(acceptableMediaTypes);
// Populate the headers in an HttpEntity object to use for the request
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
if (mediaType.equals(MediaType.APPLICATION_JSON)) {
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
} elseif (mediaType.equals(MediaType.APPLICATION_XML)) {
restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());
}
// Perform the HTTP GET request
ResponseEntity<State> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity,
State.class, abbreviation);
// Return the state from the ResponseEntity
return responseEntity.getBody();
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
@Override
protectedvoid onPostExecute(State state) {
dismissProgressDialog();
showState(state);
}
}
}