用户登录之后查询id android,Androidannotation使用之@Rest获取资源及用户登录验证(一)...

简介:

上一篇博文简单的介绍了一下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:点击获取用户:

2caa7c81ed6a3be1ea91dce8aed9e3e5.png

效果2:点击获取视频

d2f735ef610f0021da079c58dc203305.png

思考:

以第二个获取视频信息为例:如果用户没有登录,结果会怎么样呢?我们把下列代码注释掉:

String string = videoService.login("admin", "admin");

然后,在点击获取视频按钮之后,页面如下:

9a0d0d4e2ae8df44c5c8c17206e6a22f.png

明显是一个跳转到首页之后的html代码。由此可见,我们必须先登录,而且在登录的方法前面,添加注解

@SetsCookie("JSESSIONID")

但是,又有人会想,你为什么在SetCookie注解中添加“JSESSIONID”,而不是其他的值呢????其实,刚开始我也不知道怎么回事,我就发现使用"session"登不上。由于服务器也是我发开的,我直接使用浏览器的时候,需要首先进入登录页面,然后在浏览器中访问action,输入:http://localhost:8080/cbvr/getVideoInfoList.action?page=10&rows=1

此时,可以获取数据,在使用Chrome浏览器进行调试(F12),看到了访问的时候,协议原来是这样的:

97441054fc944efd61b2fe65d9f21d79.png

注意观察,在Request Headers中有一个Cookie选项,里面需要一个JSESSIONID的值,由此想到了添加Cookie相关的注解,问题得到解决。

整个项目下载:http://download.csdn.net/detail/nuptboyzhb/7242421

未经允许不得用于商业目的

ContentProvider之通过ContentResolver获取图像、视频、音频举例

MediaStore中定义了一系列的数据表格,通过ContentResolver提供的查询接口,我们可以得到各种需要的媒体信息。通过以下两个URI可以扫描设备外部和内部的

Android布局中的常用属性小结

相对布局时经常用到android:layout_above将该控件的底部至于给定ID的控件之上android:layout_below将该控件的顶部至于给定ID的控件之下android:layout_toLeftOf将该控

ListView布局之View复用原理举例

1.简介:ListView是android开发中常用的控件,系统自带的那些样式,我就不列举了。今天主要看一下,一个模仿系统历史通话记录的ListView。效果如下:上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值