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. warning: directory not found for option &#39; &#39;

    解决: 选择项目名称-->Targets-->Build Settings-->Search Paths-->Library Search Paths 删除相应路径

  2. ecshop中getAll ,getOne ,getRow的区别

    ecshop的数据库抽象层其实就是在模仿adodb $GLOBALS['db']->getAll($sql);//以二维关联数组返回所有数据 $GLOBALS['db']->getOne( ...

  3. 【JavaEE基础】在Java中如何使用jdbc连接Sql2008数据库

    我们在javaEE的开发中,肯定是要用到数据库的,那么在javaEE的开发中,是如何使用代码实现和SQL2008的连接的呢?在这一篇文章中,我将讲解如何最简单的使用jdbc进行SQL2008的数据库的 ...

  4. json级联城市

    代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> < ...

  5. Connecting Docker for Cloud Services using SDN and Network Virtualization

     Abstract The explosive scale of container CPUs needs highly efficient network virtualization Chal ...

  6. 将jar要么aar公布到到网mvn 在(使用github作为仓库), 通过gradle dependency 信息集成

    使用Android Studio用户开发,都希望通过maven该方式整合远程仓库jar.aar文件.但如何将这些文件发布它? 发人员都会将jar文件公布到sonatype上,以提供给其它开发人员集成, ...

  7. 【Android进阶】Gson解析json字符串的简单应用

    在客户端与服务器之间进行数据传输,一般采用两种数据格式,一种是xml,一种是json.这两种数据交换形式各有千秋,比如使用json数据格式,数据量会比较小,传输速度快,放便解析,而采用xml数据格式, ...

  8. ffmpeg架构和解码流程分析

    转 一,ffmpeg架构 1. 简介 FFmpeg是一个集录制.转换.音/视频编码解码功能为一体的完整的开源解决方案.FFmpeg的 开发是基于Linux操作系统,但是可以在大多数操作系统中编译和使用 ...

  9. Threejs 它可以在建立其内部房间效果可见

    Threejs 中建立可看到其内部的房间效果 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协 ...

  10. C# WinForm多线程(一)Thread类库

    Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括 ...