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 应 ...
随机推荐
- C++设计模式之建造者模式(三)
4.引入钩子方法的建造者模式 建造者模式除了逐步构建一个复杂产品对象外.还能够通过Director类来更加精细地控制产品的创建过程.比如添加一类称之为钩子方法(HookMethod)的特殊方法来控制是 ...
- openfire插件开发的几点说明
1.关于插件的目录结构 这个网上的资料很多,但是我觉得要看懂也不太容易,我这里上一个包括了jsp和servlet的图,希望大家能马上看懂: ME的Navigator视图下的截图: build path ...
- .Net 内存溢出(System.OutOfMemoryException)
.Net 内存溢出(System.OutOfMemoryException) 在什么情况下会出现OutOfMemonryException呢? 在我们试图新建一个对象时,而垃圾收集器又找不到任何可用内 ...
- 核心ASP.NET
1.用于Web应用程序的.NET Framework ASP.NET是.NET Framework的一部分,它可以在通过HTTP请求文档是在WEB服务器上动态创建它们,在.Net Framework中 ...
- Mvc 异常处理 ajax的 和 不是ajax的!
using ImageUpload.Auth; using System; using System.Collections.Generic; using System.Linq; using Sys ...
- 微端 代码project as air 分享
分享 ^_^ 1. 使用 air , as . 2. 微端下载和更新技术 成功上线棋牌游戏.它可用于传统的游戏开发. 地址: http://download.csdn.net/detail/stone ...
- Storm具体解释一、Storm 概述
一.Storm概述 Storm是一个分布式的.可靠的.零失误的流式数据处理系统. 它的工作就是委派各种组件分别独立的处理一些简单任务.在Storm集群中处理输入流的是Spout组件,而Spo ...
- s有一天,教你开始truts2
写在前面 他也是一个java和java web新秀.此前有过接触java web发展 我想一个小项目.要熟悉struts2开发过程 一个有趣的想法源于教研室项目上的一个功能实现–自己主动识别运营商,去 ...
- 集成框架 javaweb开发平台ssmy_m(生成代码) java struts2 mybatis spring maven jquery
网页地址 http://blog.csdn.net/lpy3654321/article/details/31841573 项目设想,在项目开发中,我们的开发者大多数时间都在反复开发 相同的keywo ...
- jQuery Ajax: $.post请求示例
jQuery Ajax: $.post请求示例 leyangjun.html页面 <html> <head> <meta http-equiv="Content ...