Android Open Source - spring-for-android-starter-book Main Activity






From Project

Back to project page spring-for-android-starter-book.

License

The source code is released under:

Apache License

If you think the Android project spring-for-android-starter-book 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

package net.dahanne.spring.android.ch3.http.basic.authentication;
//from   ww  w  .ja v  a 2 s  . co m
import org.springframework.http.HttpAuthentication;
import org.springframework.http.HttpBasicAuthentication;
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.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView resultTextView = (TextView) findViewById(R.id.result_text);

    AsyncTask<String, Void, String> simpleGetTask =  new AsyncTask<String, Void, String>() {
      @Override
      protected String doInBackground(String... params) {
        // Set the username and password for creating a Basic Auth request
        HttpAuthentication authHeader = new HttpBasicAuthentication("s4a", "s4a");
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setAuthorization(authHeader);
        HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

        RestTemplate restTemplate = new RestTemplate();

        restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

        try {
            // Make the HTTP GET request to the Basic Auth protected URL
            ResponseEntity<String> response = restTemplate.exchange(params[0], HttpMethod.GET, requestEntity, String.class);
            return response.getBody();
        } catch (HttpClientErrorException e) {
            // Handle 401 Unauthorized response
          Log.e("MainActivity",e.getLocalizedMessage(),e);
          return "Wrong credentials";
        }
      }
      
      @Override
      protected void onPostExecute(String result) {
        // executed by the UI thread once the background thread is done getting the result
        resultTextView.setText(result);
      }
      
    };
    
    String url = "http://restfulapp.appspot.com/hello";
    // triggers the task; it will update the resultTextView once it is done
    simpleGetTask.execute(url);
  }
}




Java Source Code List

net.dahanne.android.google.client.AbstractAsyncActivity.java
net.dahanne.android.google.client.AbstractAsyncListActivity.java
net.dahanne.android.google.client.AbstractWebViewActivity.java
net.dahanne.android.google.client.AsyncActivity.java
net.dahanne.android.google.client.GoogleActivity.java
net.dahanne.android.google.client.GoogleProfileActivity.java
net.dahanne.android.google.client.GoogleProfileListAdapter.java
net.dahanne.android.google.client.GoogleWebOAuthActivity.java
net.dahanne.android.google.client.MainApplication.java
net.dahanne.spring.android.ch3.gzip.IfConfigMeJson.java
net.dahanne.spring.android.ch3.gzip.MainActivity.java
net.dahanne.spring.android.ch3.http.basic.authentication.MainActivity.java
net.dahanne.spring.android.ch3.message.converters.feedreader.MainActivity.java
net.dahanne.spring.android.ch3.message.converters.jackson.IfConfigMeJson.java
net.dahanne.spring.android.ch3.message.converters.jackson.IfConfigMeJson.java
net.dahanne.spring.android.ch3.message.converters.jackson.IfConfigMeRestClient.java
net.dahanne.spring.android.ch3.message.converters.jackson.IfConfigMeRestClient_.java
net.dahanne.spring.android.ch3.message.converters.jackson.MainActivity.java
net.dahanne.spring.android.ch3.message.converters.jackson.MainActivity.java
net.dahanne.spring.android.ch3.message.converters.jackson.MainActivity_.java
net.dahanne.spring.android.ch3.message.converters.simplexml.IfConfigMeXml.java
net.dahanne.spring.android.ch3.message.converters.simplexml.MainActivity.java
net.dahanne.spring.android.ch3.restful.example.recipeapp.DishType.java
net.dahanne.spring.android.ch3.restful.example.recipeapp.RecipeAbstractAsyncTask.java
net.dahanne.spring.android.ch3.restful.example.recipeapp.RecipeEditor.java
net.dahanne.spring.android.ch3.restful.example.recipeapp.Recipe.java
net.dahanne.spring.android.ch3.restful.example.recipeapp.RecipesList.java
net.dahanne.spring.android.firstexample.MainActivity.java
net.dahanne.spring.android.firstexample.MainActivity.java