移动设备上的应用程序,大部分的内容都是通过通讯模块从服务端下载而来的,Android上用来通讯的大部分都采用http或者socket这两种模式,这些通讯模式都有大量的地方组件来协助以方便使用,当然也可用原生的http请求,不过需要做一些较多代码的辅助,下面我们来看看Spring Rest Template这个组件的使用。
创建项目
引入Spring依赖包和JSON序列化组件Jackson,Jackson不是必须,可以引入其他Spring支持的序列化组件不GSON之类的,不过个人感觉Jackson还是比较靠谱一点。
看看build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'org.springframework.android:spring-android-rest-template:2.0.0.M1'
compile 'com.fasterxml.jackson.core:jackson-databind:2.6.0'
//compile 'com.google.code.gson:gson:2.3.1'
}
RestTemplate
这个类是核心并且是个泛型实现,这个类提供了几个方法,我们只需要明白一个方法就行,那就是下面这个东西
public