Androidannotation @ Rest for obtaining resources and user login verification

Source: Internet
Author: User

Introduction:

The previous blog briefly introduced the simple use of AA (AndroidAnnotation). This blog briefly introduces the use of Rest annotations.

 

1. You do not need to log on. You can directly obtain it through post or get.

This method is similar to ajax in jquery. In this experiment, the server is a system developed using Struts + Spring + Hibernate. The following is an example of getting user information:

 

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. You need to log on to obtain the relevant information.

 

For example, when you request an action from the server, the server first checks whether the user has logged on. If not, the server directly jumps to the logon page. That is, the action you requested is intercepted.

Note: For more information about server interceptor, see blog: http://blog.csdn.net/nupt123456789/article/details/18504615.

The following uses obtaining video information as an example. That is, you must log on to the video before accessing the video.

 

/** $ 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. setsCook Ie; 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 {/*** to obtain the video list * You must input a JSESSIONID *. That is, you must log on to the * @ param page * @ param ro Ws * @ return */@ Post (/getVideoInfoList. action? Page = {page} & rows = {rows}) @ RequiresCookie (JSESSIONID) String getVideoInfoList (int page, int rows);/*** log on to the system, set JSESSIONID * To cookie * @ param username * @ param password * @ return */@ Post (/login. action? Username = {username} & password = {password}) @ SetsCookie (JSESSIONID) String login (String username, String password );}

Finally, let's take a look at how MainActivity is called.

 

 

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 = incluest; @ ViewByIdButton getUser; @ ViewByIdButton getVideo; @ ViewByIdTextView myTextView; @ RestServiceUserService userService; @ RestServiceVideoService videoService; /*** get image list */@ Clickvoid getUser () {getUserInBackground () ;}@ Clickvoid getVideo () {getVideoInBackground ();} /*** log on to obtain the Video list */@ Backgroundvoid getVideoInBackground () {String string = videoService. login (admin, admin); Log. d (TAG, string); String string2 = videoService. getVideoInfoList (1, 5); Log. d (TAG, string2); displayTextView (string + string2);}/*** get user list * login not required */@ Backgroundvoid getUserInBackground () {String string = userService. getUserInfoList (); Log. d (TAG, string); displayTextView (string) ;}@ UiThreadvoid displayTextView (String string) {myTextView. setText (string );}}

Effect 1: Click to get users:

 

Effect 2: Click to get the video

Thoughts:

Take the second video retrieval as an example: What will happen if the user does not log on? Comment out the following code:

 

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

Then, after clicking the get video button, the page is as follows:

 

It is obviously an html code that jumps to the homepage. Therefore, we must log on first and add annotations before the logon method.

 

@SetsCookie(JSESSIONID)

However, some people may wonder why you add "JSESSIONID" in the SetCookie annotation instead of other values ???? In fact, I didn't know what was going on at the beginning, and I found that I couldn't log on to the server using the session. Because the server was also opened by me, When I directly use the browser, I need to first enter the login page, then access the action in the browser, enter: http: // localhost: 8080/cbvr/getVideoInfoList. action? Page = 10 & rows = 1

 

In this case, you can obtain the data and debug it using the Chrome browser (F12). When you see the access, the Protocol is as follows:

Note that there is a Cookie option in Request Headers, which requiresJSESSIONIDTo add Cookie-related annotations. The problem is solved.

Entire project download: http://download.csdn.net/detail/nuptboyzhb/7242421

Not for commercial purposes without permission

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.