Mobile Services批量提交数据

来源:互联网 发布:上海图畅云计算招聘 编辑:程序博客网 时间:2024/06/10 07:26

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;    }}

服务端的工作到此完成。

客户端新建两个类,分别如下:

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() {}@Overridepublic 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;}@Overridepublic 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的初始化自己加上即可。其他客户端的开发其实是类似的,可以查看英文原文。当然,里面的ToDoItem[] todos可以改成ArrayList<ToDoItem> todos。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 别克昂科拉一公里9毛怎么办? 雷诺科雷傲车钥匙丢了怎么办 奥迪a6l烧机油了怎么办 卡地亚手镯刮花怎么办 卡地亚戒指花了怎么办 手表摔了不走了怎么办 ck手表表扣很难打开怎么办 小天才泡了水怎么办 小天才手表掉水里了怎么办 小天才电话手表进水了怎么办 小天才手表进水了怎么办 小天才电话手表丢了怎么办 小天才电话手表黑屏怎么办 安全守护注册码忘记了怎么办 儿童安全锁的门打不开怎么办 守护宝老年机打不开了怎么办 小米电话手表坏了怎么办 小米手表带坏了怎么办 小米电话手表屏幕坏了怎么办 雷诺梅甘娜06款系统错乱怎么办 轿车碰了一个坑怎么办 车子卖了没过户怎么办 9岁儿童肠胃痉挛怎么办 鹿角胶水放多了怎么办 打胰岛素血糖还是高怎么办 血糖高打胰岛素降不下去怎么办 儿童低烧37度1怎么办 小孩发烧一会冷一会热怎么办 月子里得的风湿怎么办 腰窝中间凸起肉怎么办 线雕后一个月脸发红肿胀怎么办 雷诺氏病手指僵硬疼怎么办 哺乳妈妈吃咸了怎么办 吃流产药哺乳了怎么办 坐月子吃咸了怎么办啊 拔罐放血后头晕怎么办 e道航界面不动怎么办 微信必须打开位置权限怎么办 魅族sim卡未启用怎么办 苹果6sgps信号弱怎么办 苹果5s4g网络慢怎么办