zoukankan      html  css  js  c++  java
  • 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。

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

  • 相关阅读:
    2021年Mysql个税计算公式,自定义函数
    安装篇-安装mysql8
    安装篇-安装Nginx
    jsconfig.json配置Webpack别名,识别@
    Avue动态校验表单的必填校验
    renren开源把时间类型Date换为LocalDate报错
    Avue的CRUD最强封装(三)
    Avue-curd通用模板(二)
    Kalman Filter算法详解
    STM32 ADC DMA 中断模式多通道读取ADC转换值
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4652370.html
Copyright © 2011-2022 走看看