下文仅仅是简单实现,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的登录注冊的更多相关文章

  1. How to use VS2012 remote debug Windows Azure Cloud Services

    Background: Windows Azure Cloud Services 可以在本地调试,使用Visual Studio 2012 + 模拟器 Emulator.但是模拟器的工作状态和环境和真 ...

  2. Windows Azure Virtual Machine (25) 使用SSH登录Azure Linux虚拟机

    <Windows Azure Platform 系列文章目录> 本文介绍内容适合于Azure Global和Azure China 为什么使用SSH登录Azure Linux虚拟机? 我们 ...

  3. Windows Azure - App Services

    1. 需要了解的概念:App Service Plan, Resource Group 2. Create an ASP.NET web app in Azure App Services 3. Cr ...

  4. 黑马day07 登录注冊案例(一)

    简单介绍:依据三层架构的思想设计本案例. 1.搭建好开发环境 准备好须要的包和模拟数据库配置文件users.xml -->cn.itheima.dao -->cn.itheima.serv ...

  5. 如何使用新浪微博账户进行应用登录验证(基于Windows Azure Mobile Service 集成登录验证)

    使用三方账号登录应用应该对大家来说已经不是什么新鲜事儿了,但是今天为什么还要在这里跟大家聊这个话题呢,原因很简单 Windows Azure Mobiles Service Authenticatio ...

  6. [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 ...

  7. [转]Windows Azure上安装SharePoint 2013

    基于Windows Azure 安装SharePoint 2013 前段时间写的基于Windows Azure安装SharePoint系列,由于Azure的体验账号过期了,所以不得不暂停.今天有幸参加 ...

  8. 利用 Windows Azure 实现“云优先”

    根据 IDC 的调查,云计算无疑为我们的合作伙伴提供了巨大的机会,预计到 2016 年,全球企业将在公共云服务上耗资 980 亿美元.在今天的休斯敦全球合作伙伴大会上,我们非常高兴能与合作伙伴共同寻求 ...

  9. [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 ...

随机推荐

  1. linux驱动杂项

    linux驱动 结构体中的逗号 http://zhouyang340.blog.163.com/blog/static/3024095920123495051607/ 下面我们看一个例子,Linux- ...

  2. 知识共享图文直播---(一)将数据库中的数据加载到MSFlexGrid空间中再导入Excel

    熟话说万物皆有其存在的道理,为什么我突然想写<知识共享图文直播>这个系列呢?首先,我想的是记录自己学习的历程,在记录中加深自己对知识的理解,同时也希望自己的博文能帮助到其他数据库的初学者. ...

  3. Struts2(四)Action一接收参数

    一.属性接收参数并输出 导入struts2的包,导入需要的包 和struts.xml配置文件 <?xml version="1.0" encoding="UTF-8 ...

  4. HDOJ 4686 Arc of Dream 矩阵高速幂

    矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/ ...

  5. Chrome 对于 glyphicon 字体图标不显示的解决的方法

    在将Chome默认字体渲染为微软雅黑后,部分字体图标显示为方框,这里Chome扩展文档提供的解决的方法为: 找到  custom.css 文件,路径为: C:\Users\(username)\App ...

  6. Fiddler SessionFlags

    Each Session object in Fiddler contains a collection of string flags, in the Session.oFlags[] collec ...

  7. rpm常用命令及rpm参数介绍

    RPM是RedhatPackageManager的缩写,是由RedHat公司开发的软件包安装和管理程序,同Windows平台上的Uninstaller比较类似.使用RPM,用户可以自行安装和管理Lin ...

  8. 建立与读取.ini文件

    一般读写ini文件被读写Registry所取代,但我们还是可以透过 win31的传统方式读写ini文件,以存程式目前的相关设定,而於下一次程式执行时再 读回来.目前建议使用GetSetting Sav ...

  9. MySQL工具1:mysqladmin

    每两秒显示一下MySQL的状态,一共显示5次. # mysqladmin -uroot -p -i 2 -c 5 status 查看MySQL的运行状态: #mysqladmin -uroot -p ...

  10. 解决java.lang.OutOfMemoryError: unable to create new native thread问题

    解决:1.升级JVM到最新的版本 最新版本的JVM一般在内存优化方面做的更好,升级JVM到最新的版本可能会缓解测问题2.从操作系统层面去解决 使用64位操作系统 如果使用32位操作系统遇到unable ...