Android Open Source - spring-android-samples Http Get Parameters Activity






From Project

Back to project page spring-android-samples.

License

The source code is released under:

Apache License

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.
 */*from   w  w  w.ja v  a  2s .  c  om*/
 * 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
 */
public class HttpGetParametersActivity extends AbstractAsyncActivity {

  protected static final String TAG = HttpGetParametersActivity.class.getSimpleName();

  // ***************************************
  // Activity methods
  // ***************************************
  @Override
  public void 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() {
      public void 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() {
      public void onClick(View v) {
        new DownloadStateTask().execute(MediaType.APPLICATION_XML);
      }
    });
  }

  // ***************************************
  // Private methods
  // ***************************************
  private void 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
  // ***************************************
  private class DownloadStateTask extends AsyncTask<MediaType, Void, State> {

    private String abbreviation;

    @Override
    protected void 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());
        } else if (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
    protected void onPostExecute(State state) {
      dismissProgressDialog();
      showState(state);
    }

  }

}




Java Source Code List

org.springframework.android.basicauth.AbstractAsyncActivity.java
org.springframework.android.basicauth.MainActivity.java
org.springframework.android.basicauth.Message.java
org.springframework.android.facebookclient.AbstractAsyncActivity.java
org.springframework.android.facebookclient.AbstractAsyncListActivity.java
org.springframework.android.facebookclient.AbstractWebViewActivity.java
org.springframework.android.facebookclient.AsyncActivity.java
org.springframework.android.facebookclient.FacebookActivity.java
org.springframework.android.facebookclient.FacebookFeedListAdapter.java
org.springframework.android.facebookclient.FacebookHomeFeedActivity.java
org.springframework.android.facebookclient.FacebookProfileActivity.java
org.springframework.android.facebookclient.FacebookProfileListAdapter.java
org.springframework.android.facebookclient.FacebookWallPostActivity.java
org.springframework.android.facebookclient.FacebookWebOAuthActivity.java
org.springframework.android.facebookclient.MainApplication.java
org.springframework.android.reader.AbstractAsyncListActivity.java
org.springframework.android.reader.AbstractMenuActivity.java
org.springframework.android.reader.AtomActivity.java
org.springframework.android.reader.AtomFeedActivity.java
org.springframework.android.reader.AtomFeedListAdapter.java
org.springframework.android.reader.AtomSyndFeedActivity.java
org.springframework.android.reader.MainActivity.java
org.springframework.android.reader.RssActivity.java
org.springframework.android.reader.RssChannelActivity.java
org.springframework.android.reader.RssChannelListAdapter.java
org.springframework.android.reader.RssSyndFeedActivity.java
org.springframework.android.reader.SyndFeedListAdapter.java
org.springframework.android.showcase.AbstractAsyncActivity.java
org.springframework.android.showcase.AbstractAsyncListActivity.java
org.springframework.android.showcase.AbstractMenuActivity.java
org.springframework.android.showcase.AsyncActivity.java
org.springframework.android.showcase.MainActivity.java
org.springframework.android.showcase.rest.GoogleSearchActivity.java
org.springframework.android.showcase.rest.GoogleSearchGsonActivity.java
org.springframework.android.showcase.rest.GoogleSearchJacksonActivity.java
org.springframework.android.showcase.rest.GoogleSearchResponse.java
org.springframework.android.showcase.rest.GoogleSearchResultListAdapter.java
org.springframework.android.showcase.rest.GoogleSearchResult.java
org.springframework.android.showcase.rest.HttpGetActivity.java
org.springframework.android.showcase.rest.HttpGetGzipActivity.java
org.springframework.android.showcase.rest.HttpGetGzipCompressedActivity.java
org.springframework.android.showcase.rest.HttpGetGzipCompressedJsonActivity.java
org.springframework.android.showcase.rest.HttpGetGzipUncompressedActivity.java
org.springframework.android.showcase.rest.HttpGetJsonActivity.java
org.springframework.android.showcase.rest.HttpGetJsonXmlActivity.java
org.springframework.android.showcase.rest.HttpGetParametersActivity.java
org.springframework.android.showcase.rest.HttpGetSetRequestTimeoutActivity.java
org.springframework.android.showcase.rest.HttpGetXmlActivity.java
org.springframework.android.showcase.rest.HttpPostActivity.java
org.springframework.android.showcase.rest.HttpPostFormDataActivity.java
org.springframework.android.showcase.rest.HttpPostJsonXmlActivity.java
org.springframework.android.showcase.rest.HttpPostMultiValueMapActivity.java
org.springframework.android.showcase.rest.HttpPostStringActivity.java
org.springframework.android.showcase.rest.Message.java
org.springframework.android.showcase.rest.StateList.java
org.springframework.android.showcase.rest.State.java
org.springframework.android.showcase.rest.StatesListAdapter.java
org.springframework.android.showcase.rest.TweetListAdapter.java
org.springframework.android.showcase.rest.Tweet.java
org.springframework.android.showcase.rest.TwitterSearchResults.java
org.springframework.android.twitterclient.AbstractAsyncActivity.java
org.springframework.android.twitterclient.AbstractAsyncListActivity.java
org.springframework.android.twitterclient.AbstractWebViewActivity.java
org.springframework.android.twitterclient.AsyncActivity.java
org.springframework.android.twitterclient.MainApplication.java
org.springframework.android.twitterclient.TwitterActivity.java
org.springframework.android.twitterclient.TwitterDirectMessageActivity.java
org.springframework.android.twitterclient.TwitterProfileActivity.java
org.springframework.android.twitterclient.TwitterProfileListAdapter.java
org.springframework.android.twitterclient.TwitterTimelineActivity.java
org.springframework.android.twitterclient.TwitterTimelineListAdapter.java
org.springframework.android.twitterclient.TwitterTweetActivity.java
org.springframework.android.twitterclient.TwitterWebOAuthActivity.java
org.springframework.android.twittersearch.TweetListAdapter.java
org.springframework.android.twittersearch.Tweet.java
org.springframework.android.twittersearch.TwitterSearchActivity.java
org.springframework.android.twittersearch.TwitterSearchResultsActivity.java
org.springframework.android.twittersearch.TwitterSearchResults.java
showcase.Application.java
showcase.Application.java
showcase.FileUploadController.java
showcase.HomeController.java
showcase.HomeController.java
showcase.Message.java
showcase.Message.java
showcase.StateList.java
showcase.State.java
showcase.TimerController.java
showcase.WebSecurityConfiguration.java