`
zhangym124
  • 浏览: 342623 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring for Android 探究

阅读更多

Spring for Android是什么?

  • Spring for Android 是一个Spring框架的扩展,目标是简化本地Android应用开发。

  • 特性:
    一、一个针对Android的REST客户端
    二、对于访问安全API的一个认证的支持

Spring for Android能为开发者带来什么?

就笔者而言,目前针对特性一体会到的好处主要有两点:

  • 1、REST客户端调用模板:方便与提供REST服务的服务端进行交互。

  • 2、JSON&XML解析:目前,客户端与服务端进行数据交换的格式基本全部采用JSON或XML,Spring for Android可以方便的进行JSON或XML数据的解析。

代码示例

......

    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);
        }

    }
  • RestTemplate类是Spring for Android RestTemplate库的核心。从概念上来说,它类似于其他Spring项目中的模板类。RestTemplate的行为是通过提供回调方法并配置HttpMessageConverter(用于将对象序列化为HTTP请求体以及将响应反序列化为对象)来定制的。在创建新的RestTemplate实例时,构造方法会创建出几个支持对象来实现RestTemplate的功能。

  • 对象到JSON的序列化:在Spring for Android RestTemplate中,对象到JSON的序列化需要使用第三方的JSON映射库。Spring for Android支持两个库,一个是Jackson JSON Processor,另一个是Google Gson。虽然Jackson是个众所周知的JSON解析库,但Gson库却更小,这样在打包时Android应用的体积就会更小。
    代码示例如下:

利用Jackson库:

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

利用Gson库:

restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
  • 对象到XML的序列化也需要使用第三方的XML映射库。Simple XML serializer用于提供该序列化功能。 代码示例如下:
restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());
0
0
分享到:
评论
1 楼 sdylag 2013-05-05  
 

厉害,学习了。感谢分享。继续关注Spring for Android ....以及androidannotations。。。。

 

相关推荐

    Spring For Android初体验

    《Spring for Android初体验》 在移动开发领域,Android开发者通常使用Java或Kotlin进行应用构建,但随着技术的发展,Spring框架也开始涉足Android世界。Spring for Android是Spring.io推出的一个库,旨在帮助...

    Spring for Android

    Spring for Android jar包 doc文档 源码 1.0.0M3版 Spring for Android jar包 doc文档 源码 1.0.0M3版

    Spring for Android API(Spring for Android 开发文档).CHM

    Spring for Android。 官网 Spring for Android API。 Spring for Android 开发文档。

    Spring+Android框架

    7. **Spring Cloud for Android**:虽然不直接包含在Spring for Android中,但可以将Spring Cloud的概念应用到Android,实现微服务架构,提高应用的扩展性和灵活性。 通过学习和实践"Spring+Android框架",开发者...

    Spring For Android 官方例子

    Spring For Android是Spring框架的一个扩展,其主要目的在乎简化Android本地应用的开发,这其中包括了你可以使用该项目提供的RestTemplate来为你的Android客户端提供REST服务,另外该项目还提供了跟其它诸如Twitter...

    spring-android-core-1.0.0.RELEASE-sources.jar

    《Spring for Android Core 1.0.0.RELEASE 源码解析》 Spring框架作为Java领域的重量级选手,早已深入人心。然而,Spring并不仅仅局限于服务端开发,它也拓展到了移动领域,推出了Spring for Android。这个框架的...

    spring-android-samples

    《Spring for Android实战案例解析》 在Android开发中,Spring框架的应用越来越广泛,尤其是在网络通信和数据处理方面。本文将围绕"spring-android-samples"这一主题,深入探讨Spring框架在Android平台上的应用,...

    spring for android 源码

    spring for android 源码

    Spring for Android 2 中文 参考手册 中文文档

    Spring for Android 是一个项目,旨在将Spring Framework的功能扩展到Android平台上。这样,开发者可以在Android应用中使用Spring Framework,特别是利用RestTemplate作为Android应用的REST客户端,从而能够更便捷地...

    Android代码-Spring for Android演示

    Spring for Android Samples This repository contains sample projects illustrating usage of Spring for Android. Each project requires set up of the Android SDK, command line tools, Maven, the Android ...

    Spring-android jar包及doc

    Spring-android 是一个专门为Android平台设计的轻量级框架,它将Spring框架的优秀特性引入到移动开发中,帮助开发者更高效、更灵活地构建Android应用。这个资源包含的"spring-android-1.0.0.RELEASE"是Spring-...

    spring for android

    spring for android官方demo

    用spring构建android互联网应用

    首先,`libs.zip`可能包含了Spring for Android所需的库文件,这些库如Spring Android Core、Spring Android REST Template等,是构建Android应用与服务器端交互的基础。Spring Android Core提供了一些基本服务,如...

    spring-android-1.0.0.M3

    《Spring for Android 1.0.0.M3 深度解析》 Spring for Android 是著名的企业级开发框架Spring Framework的扩展,专为Android平台设计,旨在提供一套完整的、一致的开发工具集,使得Android开发者可以利用Spring的...

    spring-android-core-1.0.0.RELEASE.jar

    Spring for android,案桌端的rest客户端,在传递rest信息和解析JSON很有帮助

    Android代码-Android版Spring框架

    Spring for Android Spring for Android is a library that is designed to provide components of the Spring Framework family of projects for use in native Android applications. Code of Conduct This ...

    spring android M4

    Spring for Android是Spring框架的一个扩展,专为移动应用开发而设计。这个项目旨在将Spring的灵活性和强大功能引入Android平台,让开发者能够利用Spring的IoC(Inversion of Control)和AOP(Aspect-Oriented ...

    spring android sample 例子

    1. **Spring for Android**:Spring框架并非专门为Android设计,但Spring for Android项目允许开发者在Android应用中利用Spring的核心特性,如依赖注入(Dependency Injection)、数据访问抽象和REST客户端支持。...

    android的spring框架

    Spring for Android是Spring.io针对Android平台推出的一个框架,旨在帮助开发者构建高质量、可测试和模块化的应用程序。下面将详细介绍Spring框架在Android中的应用和相关知识点。 1. **依赖注入(DI)** Spring框架...

    spring-android-rest-template-1.0.1.RELEASE.jar.zip

    Spring框架,作为Java开发的基石,也扩展到了Android平台,提供了Spring for Android的组件,其中Spring Android REST Template是核心工具之一。本文将深入探讨Spring Android REST Template 1.0.1.RELEASE版本,...

Global site tag (gtag.js) - Google Analytics