Androidannotation使用之@Rest获取资源及用户登录验证(一)
2014-04-24 11:10
477 查看
简介:
上一篇博文简单的介绍了一下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未经允许不得用于商业目的
上一篇博文简单的介绍了一下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未经允许不得用于商业目的
相关文章推荐
- Android 使用.net开发的webservice做用户登录验证
- 快速开发android应用2-使用TextInputLayout实现用户登录及验证
- Android中使用webservice验证用户登录的示例
- easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)
- Android中使用webservice验证用户登录的示例
- Android中使用webservice验证用户登录的示例
- Django框架下使用ajax模拟用户登录验证
- 使用SSH框架实现用户登录验证
- python-使用用户与密码登录验证的检索特定字段ver1(含文件操作知识)
- 使用MVC模型的用户登录及注册(注册验证)
- j2ee 简单网站搭建:(七)使用 shiro 结合 jcaptcha 实现用户验证登录
- 使用INNER JOIN ON 多表关联查询,获取指定用户下指定权限的资源SQL:
- Android调用.NET webservice实现用户登录验证
- AR系列路由器使用SSH用户验证方式为password登录路由器的典型配置
- 【Android应用开发详解】第01期:第三方授权认证(一)实现第三方授权登录、分享以及获取用户资料
- 单点登录(五)cas改造——使用jdbc进行用户验证
- 使用jquery1.2.6验证用户登录和注册总结
- 使用AjaxPro框架实现无刷新用户登录验证【原创】
- android使用获取所有应用的资源id和uri