下文仅仅是简单实现,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. 算法笔记_121:蓝桥杯第六届省赛(Java语言C组部分习题)试题解答

     目录 1 隔行变色 2 立方尾不变 3 无穷分数 4 格子中输出 5 奇妙的数字 6 打印大X   前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~ 1 隔行变色 隔行变色 ...

  2. 算法笔记_179:历届试题 数字游戏(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 栋栋正在和同学们玩一个数字游戏. 游戏的规则是这样的:栋栋和同学们一共n个人围坐在一圈.栋栋首先说出数字1.接下来,坐在栋栋左手边的同学要 ...

  3. TP框架中APP_SUB_DOMAIN_DEPLOY什么意思?

    'APP_SUB_DOMAIN_DEPLOY' => false, // 是否开启子域名部署 thinkphp开启域名部署/子域名部署/泛域名部署/IP访问部署            Think ...

  4. jquery 圆形进度条

    最近手里面的项目需要完成这个对设备性能的检测显示功能,需要使用到圆形进度条这样的效果,网上找了一圈,有很多相当的插件,找到:circliful 插件,看了他的使用说明比较的方便,于是就下载了它并将自己 ...

  5. 文本框input:text

      文本框 CreateTime--2017年4月24日10:40:40 Author:Marydon 一.文本框 (一)标签 <input type="text"/> ...

  6. oracle 插入表数据的4种方式

      1.往表中插入一整行数据 /*方法一*/ INSERT INTO 表名 VALUES(val1,val2,val3,...); /*方法二*/ '; 如: ,, FROM DUAL; 注意: 2. ...

  7. KnockoutJS + My97DatePicker

    如何将Knockoutjs和其他脚本库结合使用?这里给出一个Knockoutjs与my97datepicker配合使用的例子,例子中使用了ko的自定义绑定功能: ko.bindingHandlers. ...

  8. 插入UUID,出现Data truncation: Data too long for column 'id' at row 1

    ssc.udf.register("getuuid", () => UUID.randomUUID().toString) val stuPCountDF_tmp1=ssc. ...

  9. 微信小程序条码、二维码生成模块

    代码地址如下:http://www.demodashi.com/demo/13994.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  10. Android 线性布局(LinearLayout)相关官方文档 - 指南部分

    Android 线性布局(LinearLayout)相关官方文档 - 指南部分 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用 ...