Mobile Services批量提交数据,參考了文章:Inserting
multiple items at once in Azure Mobile Services
。里面事实上已经介绍得比較清楚了。但因为是英文。并且有些地方交待得不清楚。也没有Android的演示样例。故下文以Android版本号的开发为例作个补充。

首先在Mobile Services项目里新建AllToDoItems以及ToDoItem表。点击AllToDoItems,再点击script标签。将里面的内容替换例如以下:

function insert(item, user, request) {
var table = tables.getTable('ToDoItem');
populateTable(table, request, item.todos);
} function populateTable(table, request, films) {
var index = 0;
films.forEach(changeReleaseDate);
var insertNext = function () {
if (index >= films.length) {
request.respond(201, { id: 1, status: 'Table populated successfully' });
} else {
var toInsert = films[index];
table.insert(toInsert, {
success: function () {
index++;
if ((index % 20) === 0) {
console.log('Inserted %d items', index);
} insertNext();
}
});
}
}; insertNext();
} function changeReleaseDate(obj) {
var releaseDate = obj.ReleaseDate;
if (typeof releaseDate === 'string') {
releaseDate = new Date(releaseDate);
obj.ReleaseDate = releaseDate;
}
}

服务端的工作到此完毕。

client新建两个类。分别例如以下:

package com.example.ecodriveiot;

/**
* Represents an item in a ToDo list
*/
public class ToDoItem { /**
* Item text
*/
@com.google.gson.annotations.SerializedName("text")
private String mText; /**
* Item Id
*/
@com.google.gson.annotations.SerializedName("id")
private String mId; /**
* Indicates if the item is completed
*/
@com.google.gson.annotations.SerializedName("complete")
private boolean mComplete; /**
* ToDoItem constructor
*/
public ToDoItem() { } @Override
public String toString() {
return getText();
} /**
* Initializes a new ToDoItem
*
* @param text
* The item text
* @param id
* The item id
*/
public ToDoItem(String text, String id) {
this.setText(text);
this.setId(id);
} /**
* Returns the item text
*/
public String getText() {
return mText;
} /**
* Sets the item text
*
* @param text
* text to set
*/
public final void setText(String text) {
mText = text;
} /**
* Returns the item id
*/
public String getId() {
return mId;
} /**
* Sets the item id
*
* @param id
* id to set
*/
public final void setId(String id) {
mId = id;
} /**
* Indicates if the item is marked as completed
*/
public boolean isComplete() {
return mComplete;
} /**
* Marks the item as completed or incompleted
*/
public void setComplete(boolean complete) {
mComplete = complete;
} @Override
public boolean equals(Object o) {
return o instanceof ToDoItem && ((ToDoItem) o).mId == mId;
}
}
package com.example.ecodriveiot;

public class AllToDoItems {
@com.google.gson.annotations.SerializedName("id")
public String id;
public String status;
public ToDoItem[] todos;
}

批量提交的代码例如以下:

ToDoItem item = new ToDoItem();

		item.setText("test");
item.setComplete(false); ToDoItem[] items = new ToDoItem[2];
items[0]=item;
items[1]=item;
// Insert the new item
/*mToDoTable.insert(item, new TableOperationCallback<ToDoItem>() { public void onCompleted(ToDoItem entity, Exception exception, ServiceFilterResponse response) { if (exception == null) {
if (!entity.isComplete()) {
mAdapter.add(entity);
}
} else {
createAndShowDialog(exception, "Error");
} }
});*/
AllToDoItems allToDoItems = new AllToDoItems();
allToDoItems.todos=items;
mClient.getTable(AllToDoItems.class).insert(allToDoItems, new TableOperationCallback<AllToDoItems>() { public void onCompleted(AllToDoItems entity, Exception exception, ServiceFilterResponse response) { if (exception == null) {
Log.i("Debug", "status:"+entity.status);
} else {
createAndShowDialog(exception, "Error");
}
}
});

上面的代码事实上是在sdk demo的基础上改的,mClient的初始化自己加上就可以。其它client的开发事实上是类似的,能够查看英文原文。当然,里面的ToDoItem[] todos可以改变的ArrayList<ToDoItem> todos。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Mobile Services 提交批量数据的更多相关文章

  1. SQL Server 利用批量(batchsize)提交加快数据生成/导入

    在最小化日志操作解析,应用的文章中有朋友反映生成测试数据较慢.在此跟大家分享一个简单的应用,在生成数据过程中采用批量提交的方式以加快数据导入. 此应用不光生成测试数据上,在BCP导入数据中,复制初始化 ...

  2. Azure Mobile Services的REST API调用方式和自定义API

    Azure Mobile Services(移动服务)是微软在Azure平台中提供的一种跨平台的移动应用后端服务,即移动后端即服务.支持.NET和JavaScript(Node.js)写后端代码:支持 ...

  3. C#利用SqlDataAdapte对DataTable进行批量数据操作

    C#利用SqlDataAdapte对DataTable进行批量数据操作,可以让我们大大简化操作数据的代码量,我们几乎不需要循环和不关心用户到底是新增还是修改,更不用编写新增和修改以及删除的SQL语句, ...

  4. PHP 在表单POST提交后数据分页实现,非GET,解决只有第一页显示正确的问题

    //PHP 在表单POST提交后数据分页实现,非GET,使用SESSION,分页代码部分不在详述,主要为POST后的 除第一页之外的显示问题 //以下为ACTION页面 内容,仅为事例,当判断到页面未 ...

  5. Mysql 函数定义及批量数据脚本

    零.说在前面 在定义函数之前 需要先将 log_bin_trust_function_creators 值设为开启,原因如下 在主从复制的两台Mysql服务器中,slaver会从master复制数据, ...

  6. Yii 开发微信 '您提交的数据无法被验证'

    使用Yii开发微信时,出现 [error][yii\web\HttpException:] exception 'yii\web\BadRequestHttpException' with messa ...

  7. 模拟提交API数据Pyqt版

    其实这个模拟提交数据之前已经写过篇: Python requests模拟登录 因为现在在做的项目中需要一个debug请求调试API,用PHP的CURL写了一个,又因Pyqt更能直观灵活的显示请求的参数 ...

  8. 解决Yii2 启用_csrf验证后POST数据仍提示“您提交的数据无法验证”

    一 CSRF 概念 CSRF(Cross-site request forgery跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XS ...

  9. asp.net 一次性提交大量数据,服务器会报错,要在 web.config 中设置一下

    web.config <?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应 ...

随机推荐

  1. hdu2222Keywords Search (特里)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  2. tomcat开始批量——setclasspath.bat

    除了上述两批,另一个重要的脚本,那是,setclasspath.bat.它主要负责查找.检查JAVA_HOME和JRE_HOME两个变量. ****************************** ...

  3. OpenStack安装与配置2

    第二部分 OpenStack安装与配置 一.引言   本章内容讲解如何在3台物理机上搭建最小化云平台,这3台机器分为称为Server1.Server2和Client1,之后的各章也是如此.Server ...

  4. 《JavaScript设计模式与开发实践》读书笔记之代理模式

    1.代理模式 代理模式是为一个对象提供一个代用品或占位符,以便控制对它的访问 1.1 一般的图片加载 var myImage=(function () { var imgNode=document.c ...

  5. Linux应用环境实战10:Bash脚本编程语言中的美学与哲学(转)

    阅读目录 一.一切皆是字符串 二.引用和元字符 三.字符串从哪里来.到哪里去 四.再加上一点点的定义,就可以推导出整个Bash脚本语言的语法了 五.输入输出重定向 六.Bash脚本语言的美学:大道至简 ...

  6. HttpURLConnection请求数据流的写入(write)和读取(read)

    URLConnection类给应用 程序 和web资源之间架设起了通信的桥梁,这些web资源通常是通过url来标记的,本文将讲述如何使用HttpURLConnection来访问web页面(发送数据流) ...

  7. JQuery日记_5.13 Sizzle选择器(六)选择器的效率

        当选择表达式不符合高速匹配(id,tag,class)和原生QSA不可用或返回错误时,将调用select(selector, context, results, seed)方法,此方法迭代DO ...

  8. Morphia采用

    Morphia配合mongoDB喜欢Hibernate关系型数据库.也能够想到Morphia于MongoDB它调用上述的基础上,mongoDB语法. 于java直接通话Morphia的api它会变得更 ...

  9. 线性表实现简单vector

    实现一个简单的vector Vector基于数组实现,可以复制并且其占用的内存可以自动回收(通过析构函数),可以调整Vector的大小,以及容量(容量的改变是通过为基本数组分配一个新的内存块,然后复制 ...

  10. 移动web:转盘抽奖(幸运大转盘)

    为了获取客户.回馈客户,平台一般会推出抽奖活动类的营销页.因此web页面中,有各式各样的抽奖效果. 格子式(九宫格),背景滚动式(数字/文字/图案),旋转式(转盘),游戏式(砸蛋/拼图...).... ...