Mobile Services 提交批量数据
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 提交批量数据的更多相关文章
- SQL Server 利用批量(batchsize)提交加快数据生成/导入
在最小化日志操作解析,应用的文章中有朋友反映生成测试数据较慢.在此跟大家分享一个简单的应用,在生成数据过程中采用批量提交的方式以加快数据导入. 此应用不光生成测试数据上,在BCP导入数据中,复制初始化 ...
- Azure Mobile Services的REST API调用方式和自定义API
Azure Mobile Services(移动服务)是微软在Azure平台中提供的一种跨平台的移动应用后端服务,即移动后端即服务.支持.NET和JavaScript(Node.js)写后端代码:支持 ...
- C#利用SqlDataAdapte对DataTable进行批量数据操作
C#利用SqlDataAdapte对DataTable进行批量数据操作,可以让我们大大简化操作数据的代码量,我们几乎不需要循环和不关心用户到底是新增还是修改,更不用编写新增和修改以及删除的SQL语句, ...
- PHP 在表单POST提交后数据分页实现,非GET,解决只有第一页显示正确的问题
//PHP 在表单POST提交后数据分页实现,非GET,使用SESSION,分页代码部分不在详述,主要为POST后的 除第一页之外的显示问题 //以下为ACTION页面 内容,仅为事例,当判断到页面未 ...
- Mysql 函数定义及批量数据脚本
零.说在前面 在定义函数之前 需要先将 log_bin_trust_function_creators 值设为开启,原因如下 在主从复制的两台Mysql服务器中,slaver会从master复制数据, ...
- Yii 开发微信 '您提交的数据无法被验证'
使用Yii开发微信时,出现 [error][yii\web\HttpException:] exception 'yii\web\BadRequestHttpException' with messa ...
- 模拟提交API数据Pyqt版
其实这个模拟提交数据之前已经写过篇: Python requests模拟登录 因为现在在做的项目中需要一个debug请求调试API,用PHP的CURL写了一个,又因Pyqt更能直观灵活的显示请求的参数 ...
- 解决Yii2 启用_csrf验证后POST数据仍提示“您提交的数据无法验证”
一 CSRF 概念 CSRF(Cross-site request forgery跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XS ...
- asp.net 一次性提交大量数据,服务器会报错,要在 web.config 中设置一下
web.config <?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应 ...
随机推荐
- linux下用shell删除三天前或者三天内的文件
说明:+n 大于 n, -n 小于 n, n 相等于 n. find / -amin -30 -ls # 查找在系统中最后30分钟访问的文件find / -atime -2 -ls # 查找在系统中最 ...
- WPF界面设计技巧(4)—自定义列表项样式
原文:WPF界面设计技巧(4)-自定义列表项样式 有前面修改按钮样式的基础,我们可以尝试来定制一个即好看又好用的 ListBox ,今天先来讲“好看”部分. 打开 Microsoft Visual S ...
- WP8中的地图和导航
原文 WP8中的地图和导航 代码示例 源文件: Media:MapSample.zip 测试基于 SDK: Windows Phone SDK 8.0 兼容于 平台: Windows Phone 8 ...
- js正则匹配html内容
1.得到网页上的链接地址: string matchString = @"<a[^>]+href=\s*(?:'(?<href>[^']+)'|"" ...
- jconsole线程面板中的阻塞总数和等待总数(转)
阻塞总数 Blocked count is the total number of times that the thread blocked to enter or reenter a monito ...
- linux 除了某个文件或某个目录以外所有删除
rm `ls | grep -v "aa"` //删除除带aa字符串的全部文件 ls | grep -v keep | xargs rm //除keep字符串的以外全删除 r ...
- 仿CSDN Blog返回页面顶部功能
只修改了2个地方: 1,返回的速度-->改成了慢慢回去.(原来是一闪而返回) 2,返回顶部图标出现的时机-->改成了只要不在顶部就显示出来.(原来是向下滚动500px后才显示) 注意:JS ...
- python学习笔记之七:魔法方法,属性
在python中,有的名称会在前面和后面加上两个下划线,由这些名字组成的集合所包含的方法称为魔法方法(或者是特殊方法).如果对象实现了这些方法中的某一个,那么这个方法会在特殊的情况下(确切地说是根据名 ...
- 编C语言单元测试框架CUnit方法库
/********************************************************************* * Author : Samson * Date ...
- SecureCRT 6.7.1 注冊机 和谐 破解 补丁 方法
之前一直在用SecureCRT 6.5.3 版本号,和谐补丁也好找,甚至中文版本号也可找到(眼下仅仅找到了SecureCRT.6.2.0) 可是换为 6.7.1 后就怎么也注冊不了了.. 没办法试了各 ...