Windows Azure Mobiles Services实现client的登录注冊
下文仅仅是简单实现,client以Android端的实现为例:
用户表Account:
package com.microsoft.ecodrive.model;
public class Account {
@com.google.gson.annotations.SerializedName("id")
public String id;
@com.google.gson.annotations.SerializedName("username")
public String username;
@com.google.gson.annotations.SerializedName("password")
public String password;
@Override
public boolean equals(Object o) {
return o instanceof Account && ((Account) o).id == id;
}
}
一、登录
1、服务端新建api:login,将其script替换例如以下:
exports.post = function(request, response) {
// Use "request.service" to access features of your mobile service, e.g.:
// var tables = request.service.tables;
// var push = request.service.push;
response.send(statusCodes.OK, { message : "post" });
};
exports.get = function(request, response) {
var myTable = request.service.tables.getTable('Account');
myTable.where({
username: request.param('username'),
password:request.param('password')
}).read({
success: checkPermissions
});
function checkPermissions(results) {
if (results.length <= 0) {
response.send(statusCodes.BAD_REQUEST, 'No such user.');
} else {
response.send(statusCodes.OK, { message : 'sucess.' });
}
}
};
2、client登录代码例如以下:
private void login(final String name,final String pwd) {
List<Pair<String, String>> parameters = new ArrayList<Pair<String, String>>();
parameters.add(new Pair<String, String>("username", name));
parameters.add(new Pair<String, String>("password", pwd));
mClient.invokeApi("login","get", parameters, APIResult.class, new ApiOperationCallback<APIResult>() {
@Override
public void onCompleted(APIResult result, Exception exception, ServiceFilterResponse response) {
int code = response.getStatus().getStatusCode();
Log.i(TAG, "StatusCode:"+code);
if (exception == null) {
showToast("Login sucess!");
} else if (code==400) {
showToast("Username or password is wrong, please try again.");
}else{
showToast(exception.getMessage());
}
}
});
}
注:因为是从项目中摘出来的。一些方法须要自己去初始化。
二、注冊
方法一:api方式
1、服务端新建api:register。将其script替换例如以下:
exports.post = function(request, response) {
var myTable = request.service.tables.getTable('Account');
myTable.where({
username: request.param('username')
}).read({
success: checkPermissions
});
function checkPermissions(results) {
if (results.length <= 0) {
var toInsert ={username:request.param('username'),password:request.param('password')};
myTable.insert(toInsert, {
success: function () {
response.send(statusCodes.OK, { message : 'Register sucess!'});
}
});
} else {
console.log('User %s Already exist.', request.param('username'));
response.send(statusCodes.BAD_REQUEST, 'Already exist.');
}
}
};
exports.get = function(request, response) {
response.send(statusCodes.OK, { message : 'Hello World!' });
};
2、client代码例如以下:
//use api
private void register1(final String name,final String pwd) {
List<Pair<String, String>> parameters = new ArrayList<Pair<String, String>>();
parameters.add(new Pair<String, String>("username", name));
parameters.add(new Pair<String, String>("password", pwd)); mClient.invokeApi("register","post", parameters, APIResult.class, new ApiOperationCallback<APIResult>() {
@Override
public void onCompleted(APIResult result, Exception exception, ServiceFilterResponse response) {
int code = response.getStatus().getStatusCode();
Log.i(TAG, "StatusCode:"+code);
if (exception == null) {
//Sucess
showToast(result.message);
} else if (code==400) {
showToast(response.getContent());
}else{
showToast(exception.getMessage());
}
}
});
}
方法二:改动Accout表的insert的script。
1、替换Accout表的insert的script例如以下:
function insert(item, user, request) {
var permissionsTable = tables.getTable('Account');
permissionsTable.where({
username: item.username
}).read({
success: checkPermissions
});
function checkPermissions(results) {
if (results.length <= 0) {
request.execute();
} else {
console.log('User %s Already exist.', item.username);
request.respond(statusCodes.BAD_REQUEST, 'User already exist');
}
}
}
2、client代码例如以下:
//use insert
private void register(final String name,final String pwd) {
Account account = new Account();
account.username = name;
account.password = pwd;
mAccountTable.insert(account,new TableOperationCallback<Account>() {
@Override
public void onCompleted(Account result, Exception exception,
ServiceFilterResponse response) {
int code = response.getStatus().getStatusCode();
Log.i(TAG, "StatusCode:"+code);
if (exception == null) {
//Sucess
showToast("Register sucess!");
} else if (code==400) {
showToast(response.getContent().replace("\"", ""));
}else{
showToast(exception.getMessage());
}
}
});
}
另,APIResult类例如以下:
package com.microsoft.ecodrive.model;
import com.google.gson.annotations.SerializedName;
public class APIResult {
@SerializedName("count")
public int mCount;
public int getCount() {
return mCount;
}
public String message;
}
Windows Azure Mobiles Services实现client的登录注冊的更多相关文章
- How to use VS2012 remote debug Windows Azure Cloud Services
Background: Windows Azure Cloud Services 可以在本地调试,使用Visual Studio 2012 + 模拟器 Emulator.但是模拟器的工作状态和环境和真 ...
- Windows Azure Virtual Machine (25) 使用SSH登录Azure Linux虚拟机
<Windows Azure Platform 系列文章目录> 本文介绍内容适合于Azure Global和Azure China 为什么使用SSH登录Azure Linux虚拟机? 我们 ...
- Windows Azure - App Services
1. 需要了解的概念:App Service Plan, Resource Group 2. Create an ASP.NET web app in Azure App Services 3. Cr ...
- 黑马day07 登录注冊案例(一)
简单介绍:依据三层架构的思想设计本案例. 1.搭建好开发环境 准备好须要的包和模拟数据库配置文件users.xml -->cn.itheima.dao -->cn.itheima.serv ...
- 如何使用新浪微博账户进行应用登录验证(基于Windows Azure Mobile Service 集成登录验证)
使用三方账号登录应用应该对大家来说已经不是什么新鲜事儿了,但是今天为什么还要在这里跟大家聊这个话题呢,原因很简单 Windows Azure Mobiles Service Authenticatio ...
- [Windows Azure] Windows Azure Web Sites, Cloud Services, and VMs: When to use which?
This document provides guidance on how to make an informed decision in choosing between Windows Azur ...
- [转]Windows Azure上安装SharePoint 2013
基于Windows Azure 安装SharePoint 2013 前段时间写的基于Windows Azure安装SharePoint系列,由于Azure的体验账号过期了,所以不得不暂停.今天有幸参加 ...
- 利用 Windows Azure 实现“云优先”
根据 IDC 的调查,云计算无疑为我们的合作伙伴提供了巨大的机会,预计到 2016 年,全球企业将在公共云服务上耗资 980 亿美元.在今天的休斯敦全球合作伙伴大会上,我们非常高兴能与合作伙伴共同寻求 ...
- [Windows Azure] How to use the Queue Storage Service
How to use the Queue Storage Service version 1.7 version 2.0 This guide will show you how to perform ...
随机推荐
- S5PV210开发系列三_简易Bootloader的实现
S5PV210开发系列三 简易Bootloader的实现 象棋小子 1048272975 Bootloader是嵌入式系统上电后第一段运行的代码.对于功能简单的处理器,可能并没有Bo ...
- 细说java中Map的两种迭代方式
曾经对java中迭代方式总是迷迷糊糊的,今天总算弄懂了.特意的总结了一下.基本是算是理解透彻了. 1.再说Map之前先说下Iterator: Iterator主要用于遍历(即迭代訪问)Collecti ...
- Discuz常见小问题-如何实现word文档转成帖子
有一些网站比如QQ空间是可以直接导入Word文件生成网页版本的,但是效果不理想 可以发现图片进来之后都是变形了的 最笨的方法是一个一个复制粘贴(当然也不需要这么麻烦,你可以打开一个word文档之后,保 ...
- Android Studio中安装Genymotion模拟器
Genymotion的安装: Genymotion无疑是目前最快最好用的模拟器.官网下载地址:https://www.genymotion.com/ 进到官网却找不到免费下载地址了,都需要money, ...
- Java从零开始学三十三四(JAVA IO-流简述)
一.流概念(stream) File类并不能对文件内容进行读写. 读文件就是指:把文件的内中的数据读取到内存中来 写文件就是指:把内存中的数据写入到文件中去. 通过什么读写文件呢?文件流. 1.1.流 ...
- 关于gcc、glibc和binutils模块之间的关系,以及在现有系统上如何升级的总结
http://blog.csai.cn/user1/265/archives/2005/2465.html 一.关于gcc.glibc和binutils模块之间的关系 1.gcc(gnu collec ...
- C++AMP介绍(一)
C++AMP介绍(一) 最后更新日期:2014-05-02 阅读前提: 环境:Windows 8.1 64bit英文版,Visual Studio 2013 Professional Update1英 ...
- Table '' is marked as crashed and should be repaired 解决方法
解决方法: 找到mysql的安装目录的bin/myisamchk工具,在命令行中输入: myisamchk -c -r ../data/mysql/user.MYI 然后myisamchk 工具会帮助 ...
- Java Method Area
ref http://blog.csdn.net/huangfan322/article/details/53220169 https://docs.oracle.com/javase/specs/j ...
- highstock K线图 深入研究
K线图,相信每个股民都不陌生,如何用SVG画好一个K线图是一个难题. 我选择用highstock做为画图组件,适当的修改了一下源码,参考了数个财经网站的案例,完成了一个不太成熟的K线图,欢迎大家批评指 ...