程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Mobile Services批量提交數據

Mobile Services批量提交數據

編輯:C++入門知識

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

服務端的工作到此完成。

客戶端新建兩個類,分別如下:

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() {

			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() {

			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的初始化自己加上即可。其他客戶端的開發其實是類似的,可以查看英文原文。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved