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.
*/*www.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 org.springframework.android.showcase.AbstractAsyncActivity;
import org.springframework.android.showcase.R;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
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
*/publicclass HttpPostMultiValueMapActivity extends AbstractAsyncActivity {
protectedstaticfinal String TAG = HttpPostMultiValueMapActivity.class.getSimpleName();
// ***************************************
// Activity methods
// ***************************************
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.http_post_multi_value_activity_layout);
// Initiate the JSON POST request when the JSON button is clicked
final Button buttonJson = (Button) findViewById(R.id.button_submit);
buttonJson.setOnClickListener(new View.OnClickListener() {
publicvoid onClick(View v) {
new PostMessageTask().execute();
}
});
}
// ***************************************
// Private methods
// ***************************************
privatevoid showResult(String result) {
if (result != null) {
// display a notification to the user with the response message
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "I got null, something happened!", Toast.LENGTH_LONG).show();
}
}
// ***************************************
// Private classes
// ***************************************
privateclass PostMessageTask extends AsyncTask<Void, Void, String> {
private MultiValueMap<String, String> message;
@Override
protectedvoid onPreExecute() {
showLoadingProgressDialog();
// assemble the map
message = new LinkedMultiValueMap<String, String>();
EditText editText = (EditText) findViewById(R.id.edit_text_message_id);
message.add("id", editText.getText().toString());
editText = (EditText) findViewById(R.id.edit_text_message_subject);
message.add("subject", editText.getText().toString());
editText = (EditText) findViewById(R.id.edit_text_message_text);
message.add("text", editText.getText().toString());
}
@Override
protected String doInBackground(Void... params) {
try {
// The URL for making the POST request
final String url = getString(R.string.base_uri) + "/sendmessagemap";
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate(true);
// Make the network request, posting the message, expecting a String in response from the server
ResponseEntity<String> response = restTemplate.postForEntity(url, message, String.class);
// Return the response body to display to the user
return response.getBody();
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
@Override
protectedvoid onPostExecute(String result) {
dismissProgressDialog();
showResult(result);
}
}
}