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 org.springframework.android.showcase.AbstractAsyncActivity;
import org.springframework.android.showcase.R;
import org.springframework.http.ContentCodingType;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
/**
* @author Roy Clarkson
*/publicclass HttpGetGzipUncompressedActivity extends AbstractAsyncActivity {
privatestaticfinal String TAG = HttpGetGzipUncompressedActivity.class.getSimpleName();
// ***************************************
// Activity methods
// ***************************************
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.http_get_gzip_activity_layout);
}
@Override
publicvoid onStart() {
super.onStart();
new UncompressedRequestTask().execute();
}
// ***************************************
// Private methods
// ***************************************
privatevoid refreshResults(ResponseEntity<String> response) {
if (response == null) {
return;
}
HttpHeaders headers = response.getHeaders();
StringBuilder sb = new StringBuilder();
sb.append("Date: ").append(headers.getFirst("Date")).append("\n");
sb.append("Status: ").append(headers.getFirst("Status")).append("\n");
sb.append("Content-Type: ").append(headers.getFirst("Content-Type")).append("\n");
sb.append("Content-Encoding: ").append(headers.getFirst("Content-Encoding")).append("\n");
sb.append("Content-Length: ").append(headers.getFirst("Content-Length")).append("\n");
TextView textView = (TextView) findViewById(R.id.text_view_headers);
textView.setText(sb.toString());
String results = response.getBody() + "\n";
textView = (TextView) findViewById(R.id.text_view_results);
textView.setText(results);
}
// ***************************************
// Private classes
// ***************************************
privateclass UncompressedRequestTask extends AsyncTask<Void, Void, ResponseEntity<String>> {
@Override
protectedvoid onPreExecute() {
showLoadingProgressDialog();
}
@Override
protected ResponseEntity<String> doInBackground(Void... params) {
try {
// The URL for making the GET request
final String url = "http://search.twitter.com/search.json?q={query}&rpp=100";
// Add the Identity Accept-Encoding header to the request
// This disables gzip compression in Gingerbread (2.3) and newer
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAcceptEncoding(ContentCodingType.IDENTITY);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
// Perform the HTTP GET request
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(
requestHeaders), String.class, "SpringSource");
return response;
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
@Override
protectedvoid onPostExecute(ResponseEntity<String> response) {
dismissProgressDialog();
refreshResults(response);
}
}
}