Androidannotation使用之@Rest获取资源及用户登录验证(一)
版权声明:本文为博主原创文章。未经博主同意不得转载。
https://blog.csdn.net/NUPTboyZHB/article/details/24384713
简介:
上一篇博文简单的介绍了一下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.须要登录后,才干获取到对应的信息。
如,当向server请求一个action的时候,server首先推断用户是否已经登录,假设没有登录,则直接跳转到登录页面。
也即是:你所请求的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");
然后。在点击获取视频button之后。页面例如以下:
明显是一个跳转到首页之后的html代码。由此可见,我们必须先登录,并且在登录的方法前面。加入注解
@SetsCookie("JSESSIONID")
可是。又有人会想,你为什么在SetCookie注解中加入“JSESSIONID”,而不是其它的值呢????事实上。刚開始我也不知道怎么回事,我就发现使用"session"登不上。因为server也是我发开的,我直接使用浏览器的时候,须要首先进入登录页面。然后在浏览器中訪问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
未经同意不得用于商业目的
最新文章
- CRUD查询
- 10 months then free? 10个月,然后自由
- 虚拟机centos6.5 --设置静态ip
- 感受机房管理化繁为简-新款KVM使用心得
- IOS 多个ImageView图片层叠透明区域点击事件穿透
- linux sudo环境变量设置
- Windows 8.1下使用IE 64位
- shell编程-1到100的求和与冒泡排序
- JavaScript中的setAttribute用法
- MindNode for mac 思维导图
- 文件描述符与FILE
- 转:H2 入门
- java类中获取ServletContext的方法
- PHP 环境搭建及zabbix安装遇到的一些坑.
- 01_搭建Linux虚拟机(下)_我的Linux之路
- Java基础1:深入理解Java面向对象三大特性
- Data_Structure01-绪论
- jquery的widget源代码剖析
- Codeforces 1009 F - Dominant Indices
- centos 卸载mysql