简介:
上一篇博文简单的介绍了一下AA(AndroidAnnotation)的简单使用,本博客简单介绍Rest注解的使用。
官方网站介绍:https://github.com/excilys/androidannotations/wiki/Rest-API#rest
1.无需登录 ,直接通过post或者get获取
该方式和jquery中的ajax基本类似,本次实验,服务端就是用Struts+Spring+Hibernate开发的系统。以下用一个获取用户信息的例子说明:
package com.example.testaa; import org.androidannotations.annotations.rest.Accept; import org.androidannotations.annotations.rest.Post; import org.androidannotations.annotations.rest.Rest; import org.androidannotations.api.rest.MediaType; import org.springframework.http.converter.StringHttpMessageConverter; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2014-4-20 Nanjing,njupt,China */ @Rest(rootUrl = "http://192.168.0.104:8080/cbvr", converters = {StringHttpMessageConverter.class }) public interface UserService { @Post("/getUserInfoList.action") @Accept(MediaType.APPLICATION_JSON) String getUserInfoList(); }
2.需要登录后,才能获取到相应的信息。
如,当向服务器请求一个action的时候,服务器首先判断用户是否已经登录,如果没有登录,则直接跳转到登录页面。也即是:你所请求的action被拦截。
注释:服务端拦截器的写法可参见博文:http://blog.csdn.net/nupt123456789/article/details/18504615
以下以获取视频信息为例,也即是用户必须在登录的前提下,才能访问。
/* * $filename: UserService.java,v $ * $Date: 2014-4-20 $ * Copyright (C) ZhengHaibo, Inc. All rights reserved. * This software is Made by Zhenghaibo. */ package com.example.testaa; import org.androidannotations.annotations.rest.Post; import org.androidannotations.annotations.rest.RequiresCookie; import org.androidannotations.annotations.rest.Rest; import org.androidannotations.annotations.rest.SetsCookie; import org.springframework.http.converter.StringHttpMessageConverter; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2014-4-20 Nanjing,njupt,China */ @Rest(rootUrl = "http://192.168.0.104:8080/cbvr", converters = {StringHttpMessageConverter.class }) public interface VideoService { /** * 获取视频列表 * 必须传入一个JSESSIONID * 也就是说,必须在登录的情况下才可以 * @param page * @param rows * @return */ @Post("/getVideoInfoList.action?page={page}&rows={rows}") @RequiresCookie("JSESSIONID") String getVideoInfoList(int page,int rows); /** * 登录系统,并将JSESSIONID * 设置到cookie中 * @param username * @param password * @return */ @Post("/login.action?username={username}&password={password}") @SetsCookie("JSESSIONID") String login(String username,String password); }
最后,我们看一下MainActivity是怎么调用的。
package com.example.testaa; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.Click; import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.UiThread; import org.androidannotations.annotations.ViewById; import org.androidannotations.annotations.rest.RestService; import android.app.Activity; import android.util.Log; import android.widget.Button; import android.widget.TextView; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2014-4-15 Nanjing,njupt,China */ @EActivity(R.layout.activity_main) public class MainActivity extends Activity { private static final String TAG="AAREST"; @ViewById Button getUser; @ViewById Button getVideo; @ViewById TextView myTextView; @RestService UserService userService; @RestService VideoService videoService; /** * 获取图像列表 */ @Click void getUser() { getUserInBackground(); } @Click void getVideo(){ getVideoInBackground(); } /** * 获取Video视频列表 * 需要登录 */ @Background void getVideoInBackground(){ String string = videoService.login("admin", "admin"); Log.d(TAG, string); String string2 = videoService.getVideoInfoList(1,5); Log.d(TAG, string2); displayTextView(string+string2); } /** * 获取用户列表 * 无需登录 */ @Background void getUserInBackground(){ String string = userService.getUserInfoList(); Log.d(TAG, string); displayTextView(string); } @UiThread void displayTextView(String string){ myTextView.setText(string); } }
效果1:点击获取用户:
效果2:点击获取视频
思考:
以第二个获取视频信息为例:如果用户没有登录,结果会怎么样呢?我们把下列代码注释掉:
String string = videoService.login("admin", "admin");
然后,在点击获取视频按钮之后,页面如下:
明显是一个跳转到首页之后的html代码。由此可见,我们必须先登录,而且在登录的方法前面,添加注解
@SetsCookie("JSESSIONID")
但是,又有人会想,你为什么在SetCookie注解中添加“JSESSIONID”,而不是其他的值呢????其实,刚开始我也不知道怎么回事,我就发现使用"session"登不上。由于服务器也是我发开的,我直接使用浏览器的时候,需要首先进入登录页面,然后在浏览器中访问action,输入:http://localhost:8080/cbvr/getVideoInfoList.action?page=10&rows=1
此时,可以获取数据,在使用Chrome浏览器进行调试(F12),看到了访问的时候,协议原来是这样的:
注意观察,在Request Headers中有一个Cookie选项,里面需要一个JSESSIONID的值,由此想到了添加Cookie相关的注解,问题得到解决。
整个项目下载:http://download.csdn.net/detail/nuptboyzhb/7242421
未经允许不得用于商业目的