近日,SpringSource发布了Spring for Android 1.0。Spring for Android是Spring Framework的扩展,有助于简化原生Android应用的开发。此次发布最为引入关注之处是提供了一个REST客户端(RestTemplate)以及对OAuth的支持(Spring Social)。

Spring for Android 1.0的首个主要特性就是REST客户端。你现在可以通过Spring RestTemplate抽象层在原生Android应用中使用RESTfule服务了。根据Google的建议,RestTemplate对 Android 2.3+使用了J2SE HTTP客户端库,对Android 2.2及之前的版本使用了HttpClient。它支持不同的HTTP消息转换器、使用Jackson或Gson实现JSON编排、使用Simple XML Serializer实现XML编排、使用Android ROME实现RSS/Atom编排。RestTemplate还支持gzip压缩。下面这个简单的RestTemplate示例来自于Spring for Android参考手册,该示例使用搜索关键词“SpringSource”来搜索Google。

String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={query}"; RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new StringHttpMessageConverter()); String result = restTemplate.getForObject(url, String.class, "SpringSource"); 

Spring for Android 1.0的另一个主要特性就是对Spring Social的支持。你现在可以在Android应用中使用Spring Social,这包括一个OAuth客户端和诸如Twitter与Facebook等流行社交站点的实现。此次发布提供了对OAuth 1.x与2.0的支持,并且包含一个SQLite库以及兼容于Android的Spring Security加密。

熟悉Spring Framework的开发者自然而然地期望Spring for Android能够支持依赖注入。然而,由于Android通过Intent来驱动应用生命周期的方式,依赖注入方式的实现会很受限。感兴趣的读者请阅读Clean Code in Android Applications来了解关于Android中使用依赖注入的更多信息。

要想了解更多信息,请参阅Spring for Android参考手册。Spring for Android的示例位于GitHub上。若想起步,请下载发布包或是添加Maven依赖。

<dependency> <groupId>org.springframework.android</groupId> <artifactId>spring-android-rest-template</artifactId> <version>1.0.0.RELEASE</version> </dependency>  <dependency> <groupId>org.springframework.android</groupId> <artifactId>spring-android-auth</artifactId> <version>${spring-android-version}</version> </dependency> 

要想在Android应用中利用Maven的依赖管理功能,请不要忘记阅读Maven依赖管理。

更多相关文章

  1. Android Activity的4种启动模式详解(示例)
  2. android客户端与服务端交互的三种方式
  3. Android 8.0 新特性
  4. android 5.0新特性之Material Design 使用Material主题
  5. Android 开源项目 eoe 社区 Android 客户端
  6. Android中的sqlite简单示例
  7. android studio 3.6.0 绑定视图新特性的方法
  8. Android Content Provider详解及示例代码
  9. android 学习示例

随机推荐

  1. Android(安卓)项目组件化之创建module,生
  2. OOM问题总结
  3. android webview使用html5 上传相册、拍
  4. android 4.0 体验!!!!
  5. Android 使用Theme实现动态切换主题详细
  6. 【平台开发】如何有效的收集 Android(安
  7. android 对超大屏幕尺寸的支持
  8. android xml布局之———include.merge.V
  9. 【Android】AsyncTask源码分析
  10. Android(安卓)判断网络类型2G,3G,4G,WIFI