[JAVA] Build Spring for Android 2.0.0 environment

How to use Spring for Android 2.0.0

The element of personal memo is strong. I'm still studying, so I'd appreciate it if you could point out any mistakes.

Create a new project in Android Studio Add the following description to the build.gradle file on the app side and sync now

dependencies {
    snip
    compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
}

Because an error occurs when trying to execute restTemplate.getForObject etc. in the main thread It seems that it is necessary to execute using asynchronous processing using AsyncTask.

Sample code It is a code that executes Zip Code Search API and displays it in textViewTest.

Processing on the side where the button is pressed ↓ The linking part such as findViewById is omitted.

@Override
    public void onClick(View view) {

        int intId;
        intId = view.getId();

        switch (intId){
            case R.id.buttonTest:

                String zipString;
                zipString = editTextZipCode.getText().toString();
                Object[] getParams = new Object[1];
                getParams[0] = zipString;

                apiTask = new ApiTask();
                apiTask.execute(getParams);
                break;
        }
    }
class ApiTask extends AsyncTask<Object, String, String> {

        @Override
        protected String doInBackground(Object[] data) {

            String zipCode = (String) data[0];

            //Execute API using Spring

            // The connection URL
            String url = "http://zipcloud.ibsnet.co.jp/api/search?zipcode={zipcode}";
            // Create a new RestTemplate instance
            RestTemplate restTemplate = new RestTemplate();

            // Add the String message converter
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

            // Make the HTTP GET request, marshaling the response to a String
            String result = restTemplate.getForObject(url,String.class,zipCode);
           
            return result;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
        }

        //Post-processing after asynchronous processing is completed
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute((String) result);
            progressBar.setVisibility(View.INVISIBLE);
            textViewTest.setText(result);

        }
    }

Recommended Posts

Build Spring for Android 2.0.0 environment
Build Java development environment (for Mac)
Build a development environment for Docker + Rails6 + Postgresql
Build Spring Boot project by environment with Gradle
Build a development environment for Docker, java, vscode
Spring boot development-development environment-
[Error resolution] Occurs when trying to build an environment for spring with docker
How to build docker environment with Gradle for intelliJ
Build debug environment for Phalcon + Vagrant + centos + xdebug + phpstorm
Build an Android image for Orange Pi 4 with Docker
[2021] Build a Docker + Vagrant environment for using React / TypeScript
[Spring Boot] Environment construction (macOS)
default value for android: exported
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Build docker environment with WSL
Docker × Spring Boot environment construction
Ruby ① Build a Windows environment
Spring Boot for annotation learning
Build WebAPP development environment with Java + Spring with Visual Studio Code
Various switching application.properties for each environment when Spring Boot starts
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
Build an environment for Rails projects under Git management in Cloud9
Build a development environment for Django + MySQL + nginx with Docker Compose
We will build a Spring Framework development environment in the on-premises environment.
Database command for migration file environment
Build Couchbase local environment with Docker
◆ Spring Boot + gradle environment construction memo
Build a Node.js environment with Docker
Spring Boot for the first time
[Android] Cause and remedy for TransactionTooLargeException
Environment construction for Servlet application development
Build a Tomcat 8.5 environment with Pleiades 4.8
Environment construction with Docker for beginners
Java Spring environment in vs Code
Links for creating Android apps (for beginners)
Build PlantUML environment with VSCode + Docker
First Android development for busy people
Library collection useful for Android development
Build Rails environment with Docker Compose
[Android] Create validation for date input!
Notes for Android application development beginners
Frequent annotations for Spring Boot tests
[Android] Procedure for supporting deep links
Spring Framework tools for Java developer
Build a XAMPP environment on Ubuntu
Use DBUnit for Spring Boot test
Build jooby development environment with Eclipse
Jupyter's Docker environment for running TensorFlow
[Java & SpringBoot] Environment Construction for Mac
Spring AOP for the first time
Build ffmpeg 4.3.1 on Ubuntu for Windows
Build Unity development environment on docker
Build docker + laravel environment with laradock
Android OS (7.1.2) build and Hello World
Spring Boot gradle build with Docker
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to build an environment for any version of Ruby using rbenv
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)