android + Spring RESTful 简单登录 (spring3实现服务端 rest api)
https://github.com/spring-projects/spring-android-samples/tree/master/spring-android-showcase
注: url?format=json 服务器端返回json数据 url?format=xml 服务器端返回xml数据 url 默认返回html数据 登录restful风格 部分源码 android客户端 导入spring for android开源jar spring-android-rest-template-1.0.0.RC1.jar spring-android-core-1.0.0.RC1.jar spring-android-auth-1.0.0.RC1.jar 服务器端部分代码(spring3.1.1+hibernate4.1.2(jpa2)完全注解方式实现 ) ======================================上代码===================================== ======================================android客户端=================================
1. public class
2.
3. private static
4.
5. private final static String url = "http://192.168.0.8:6060/contact";
6.
7. private final static String login = url + "/user/userLogin/{loginname},{loginpassword}?format=json";
8.
9. private final static String query = url + "/user/list?format=json";
10.
11. private final static String add = url + "/user/add?format=json";
12.
13. private final static String detail = url + "/user/{id}/detail?format=json";
14.
15. private final static String update = url + "/user/{id}/updates?format=json";
16.
17. private final static String delete = url + "/user/{id}/delete?format=json";
18.
19. private final static String upload = url + "/user/upload?format=json";
20.
21. static
22. new
23. }
24.
25. public
26. }
27. /**
28. * 登录,
29. * @param username
30. * @param password
31. * @return {"user":{"id":"1","loginname":"superman","loginpassword":"unicom","username":"超级管理员","useremail":"superman@163.com","usermobile":null,"userstate":null,"userlogintime":null}}
32. */
33. public static
34. new
35. "loginname", username);
36. "loginpassword", password);
37. return
38. }
39. /**
40. * 得到所有的用户,
41. * @return ........
42. */
43. public static
44. return getObject(query, null);
45. }
46. /**
47. * 根据id得到唯一用户
48. * @param id
49. * @return{"user":{"id":"4028809b376e28be01376e294b180000","loginname":"sdfsdf","loginpassword":"sdfsdf","username":"sdfsdf","useremail":"sdfsdf","usermobile":null,"userstate":null,"userlogintime":null}}
50. */
51. public static
52. new
53. "id", id);
54. return
55. }
56. /**
57. * 添加一个用户
58. * @param user
59. * @return
60. */
61. public static
62. new
63. "id", "");
64. new
65. "loginname", user.getLoginname());
66. "loginpassword", user.getLoginpassword());
67. "useremail", user.getUseremail());
68. "usermobile", user.getUsermobile());
69. return
70. }
71. /**
72. * 根据id更新用户 返回更新的实例和信息(restTemplate.put 也可以直接更新)
73. * @param id
74. * @param user
75. * @return{"users":{"id":"4028809b376daa8901376dab59520001","loginname":"45345345","loginpassword":null,"username":"测试一下kankan","useremail":null,"usermobile":"ddddd","userstate":null,"userlogintime":null}}
76. */
77. public static
78. new
79. "id", id);
80. new
81. "loginname", user.getLoginname());
82. "username", user.getUsername());
83. "usermobile", user.getUsermobile());
84. return
85. }
86. /**
87. * 更加id删除用户(也可以用restTemplate.delete 但是没有返回的信息)
88. * @param id
89. * @param user
90. * @return {"ajax":{"success":true,"messages":"删除成功!"}}
91. */
92. public static
93. new
94. "id", id);
95. return
96. }
97. /**
98. * 上传文件
99. * @param path
100. * @return
101. */
102. public static
103. new
104. Resource resource;
105. try
106. new UrlResource("file://"+path);
107. "json", resource);
108. catch
109. e.printStackTrace();
110. }
111. new
112. requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
113. new
114. class);
115. return
116. }
=============================服务器端部分代码==============================
1. /**注意下面的方法中请求的方法名称是一样的,
2. * 只不过根据不同的RequestMethod.GET来找对应的方法
3. * RequestMethod (
4. * GET=get提交,
5. * HEAD,
6. * POST=post提交,
7. * PUT=put更新,
8. * DELETE=delete删除,
9. * OPTIONS, TRACE)
10. * 对应
11. * <form:form method="POST">
12. * <form:form method="PUT">
13. * <form:form method="DELETE">
14. */
15.
16. /**
17. * 用户列表例子(不带分页)
18. * @return
19. */
20. @RequestMapping(value="/list",method=RequestMethod.GET)
21. public
22. List<Users> list = usersService.listAll();
23. return new ModelAndView("main", "userslist", list);
24. }
25. /**
26. * 用户列表例子(带分页)
27. * @return
28. */
29. @RequestMapping(value="/query",method=RequestMethod.GET)
30. public ModelAndView UserListPage(HttpServletRequest request,Model model, @ModelAttribute("command") Users command){
31. model.addAttribute(Constants.COMMAND, command);
32. int pn = ServletRequestUtils.getIntParameter(request, "pn", 1);
33. Page<Users> list = usersService.listAllUsers(command, pn);
34. "page", list);
35. return new ModelAndView("query");
36. }
37. /**
38. * 新增之前绑定PO到form表单跳转到新增页面例子
39. * @param model
40. * @return
41. */
42. @RequestMapping(value="/add",method=RequestMethod.GET)
43. public
44. //相当于request.setattribute的意思
45. if(!model.containsAttribute(Constants.COMMAND))
46. new
47. return new ModelAndView("add");
48. }
49. /**
50. * 新增用户例子
51. * @param users
52. * @return
53. */
54. @RequestMapping(value="/add",method=RequestMethod.POST)
55. public
56. usersService.save(user);
57. new
58. true);
59. "注册成功!");
60. return new ModelAndView("ajax","ajax",ajax);
61. //也可以直接返回列表也行
62. //return new ModelAndView(new RedirectView("list"));
63. }
64. /**
65. * 更新之前绑定PO到form表单跳转到更新页面
66. * @param id
67. * @param model
68. * @return
69. */
70. @RequestMapping(value="/{id}/detail",method=RequestMethod.GET)
71. public ModelAndView get_Users(@PathVariable
72. Users users = usersService.get(id);
73. "users", users);
74. return new ModelAndView("detail");
75. }
76. /**
77. * 更新之前绑定PO到form表单跳转到更新页面
78. * @param id
79. * @param model
80. * @return
81. */
82. @RequestMapping(value="/{id}/updates",method=RequestMethod.GET)
83. public ModelAndView before_UsersUpdate(@PathVariable
84. Users users = usersService.get(id);
85. if(!model.containsAttribute(Constants.COMMAND))
86. model.addAttribute(Constants.COMMAND, users);
87. return new ModelAndView("updates");
88. }
89. /**
90. * 更新用户例子(put更新 一般用在web)
91. * @param users
92. * @return
93. */
94. @RequestMapping(value="/{id}/updates",method=RequestMethod.PUT)
95. public
96. usersService.update(user);
97. return new ModelAndView("redirect:../list");
98. }
99. /**
100. * 更新用户例子(post更新 一般用在client)
101. * @param users
102. * @return
103. */
104. @RequestMapping(value="/{id}/updates",method=RequestMethod.POST)
105. public
106. usersService.update(user);
107. new
108. true);
109. "更新成功!");
110. return new ModelAndView("ajax","ajax",ajax);
111. }
112. /**
113. * 删除用户例子
114. * @param id
115. * @return
116. */
117. @RequestMapping(value="/{id}/delete",method=RequestMethod.GET)
118. public ModelAndView UserDelete(@PathVariable
119. usersService.delete(id);
120. new
121. true);
122. "删除成功!");
123. return new ModelAndView("ajax","ajax",ajax);
124. }
125. /**
126. * 上传文件
127. * @param request
128. * @return
129. */
130. @RequestMapping(value="/upload",method=RequestMethod.POST)
131. public ModelAndView Upload(@RequestParam("json") MultipartFile multi){
132. try
133. byte[] bytes = multi.getBytes();
134. new FileOutputStream("D:\\"+multi.getOriginalFilename());
135. out.write(bytes);
136. out.close();
137. catch
138. // TODO Auto-generated catch block
139. e.printStackTrace();
140. }
141.
142. new
143. true);
144. "文件上传成功!");
145. return new ModelAndView("ajax","ajax",ajax);
146. }
//--------------------------------------------------------------
使用Spring-Android获取和解析json数据
项目中需要的LIB: spring-android-core-1.0.0.M4.jar spring-android-rest-template-1.0.0.M4.jar gson-2.1.jar 使用google的这个json解析lib是因为这个lib较小 服务端返回数据如:
1. {"id":"109","word":"test","translation":"n. 试验;检验"}
客户端建立对应的Domain类,如:
1. public class
2. private int
3. private
4. private
5. }
使用Spring-Android调用,并转换成Word类
1. String url = "http://localhost/api/word/detail.php?id=109";
2. RestTemplate restTemplate = new
3. Word word = restTemplate.getForObject(url, Word.class);
另外Spirng Android的Lib库中还提供了解析XML,RSS,还有OAuth验证的实现。
//--------------------------------------------------------------------------------------
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文件如下:
|
4.MainActivity.java如下:
|