spring-android的使用

转自:http://www.chenwg.com/android/spring-android%E7%9A%84%E4%BD%BF%E7%94%A8.html

了解J2EE的人都会知道spring这个开源框架,不过哥对J2EE的开发没什么兴趣,太重量级了,不适合互联网的应用,还是喜欢php多点,不过sping在移动开发这块也推出了spring-android,spring-android可以做什么?有什么优势呢?

spring-android主要提供了两个重要的功能:

1.Rest模板,很多Android应用都要与服务器进行交互,而现在很多互联网应用的服务器端都会提供Rest服务,数据格式一般是json、xml、rss等,如果使用spring-android,这将大大方便你的Android应用与服务器端的交互,spring-android在解析json,xml都是非常方便的;

2.Auth授权验证,现在很多互联网应用都提供了开放的API服务,而你的Android应用要接入到这些服务中去,往往要经过授权才行,现在很多应用都使用Auth授权认证,如twitter、facebook、新浪微博等,如果使用spring-android,在授权验证这块将会非常方便。

如何使用spring-android呢?

1.首先要去http://www.springsource.org/spring-android 下载spring-android,然后解压。

2.新建一个Android项目,然后将解压后的spring-android里的spring-android-core-1.0.1.RELEASE.jar和spring-android-rest-template-1.0.1.RELEASE.jar放到Android项目的lib目录下,因为要访问在网络,所以要在AndroidManifest.xml文件下加入<uses-permission android:name=”android.permission.INTERNET”/>​

3.acitivity_main.xml文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
 
    <TextView
        android:id="@+id/result_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        tools:context=".MainActivity"/>
 
</RelativeLayout>

4.MainActivity.java如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.hxxy.springforandroidfirstdemo;
 
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
 
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
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) {
                // executed by a background thread
 
                // 创建一个RestTemplate实例
                RestTemplate restTemplate = new RestTemplate();
 
                // 添加字符串消息转换器
                restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
 
                return restTemplate.getForObject(params[0], String.class);
            }
 
            @Override
            protected void onPostExecute(String result) {
                resultTextView.setText(result);
            }
 
        };
 
        String url = "http://www.chenwg.com";
        // 完成时更新resultTextView
        simpleGetTask.execute(url);
    }
}
 
posted @ 2013-04-07 21:36  寒星晓月 专注移动互联网  阅读(2602)  评论(0编辑  收藏  举报