簡體   English   中英

Windows Azure網站與Android設備通信

[英]Windows Azure Website Communicate with Android Device

項目要求: Windows Azure Azure網站和使用Azureure移動服務的android設備之間的通信

1)使用Microsoft Windows Azure(服務器端)在VB.NET中構建網站

2)Android應用位於Android設備(客戶端)上

客戶端(Android用戶)應該能夠將數據從android設備(客戶端)發送到網站(服務器端),反之亦然。為了實現這一點,即客戶端與服務器之間的通訊,我正在使用Microsoft Windows Azure提供的移動服務(服務器端)使用GCM(Google雲消息傳遞)

我已經按照文檔執行了所有步驟

http://www.windowsazure.com/zh-CN/develop/mobile/tutorials/get-started-with-push-android/

還按照Microsoft Windows Azure的上述鏈接文檔中提供的所有步驟進行操作

但是當我嘗試從Android設備向網站發送消息時,發生以下錯誤

錯誤: com.microsoft.windowsazure.mobileservices.MobileServiceException:處理請求時出錯

注意:GCM(Google Cloud Messaging)為我們提供了一個gcm.jar文件,該文件在android應用中用於向Server即網站發送數據

創建代碼

           @Override
       public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_to_do);


    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    mRegistationId = GCMRegistrar.getRegistrationId(this);
    if (mRegistationId.equals("")) {
        GCMRegistrar.register(this, SENDER_ID);
    }

    mProgressBar = (ProgressBar) findViewById(R.id.loadingProgressBar);

    // Initialize the progress bar
    mProgressBar.setVisibility(ProgressBar.GONE);

    try {
        // Create the Mobile Service Client instance, using the provided
        // Mobile Service URL and key
        mClient = new MobileServiceClient(
                "url of website",
                "POBHgxwAktyxUdeRRpcFyqEcsppwiS99",
                this).withFilter(new ProgressFilter());

        // Get the Mobile Service Table instance to use
        mToDoTable = mClient.getTable(ToDoItem.class);

        mTextNewToDo = (EditText) findViewById(R.id.textNewToDo);

        // Create an adapter to bind the items with the view
        mAdapter = new ToDoItemAdapter(this, R.layout.row_list_to_do);
        ListView listViewToDo = (ListView) findViewById(R.id.listViewToDo);
        listViewToDo.setAdapter(mAdapter);

        // Load the items from the Mobile Service
        refreshItemsFromTable();
                    }
                    catch (MalformedURLException e) {
                    createAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
                }
                }

在按鈕上單擊事件addItem()被調用

            public void addItem(View view) {
    if (mClient == null) {
        return;
    }

    try
    {

    // Create a new item
    ToDoItem item = new ToDoItem();

    item.setText(mTextNewToDo.getText().toString());
    item.setComplete(false);

    // 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");
            }

        }
    });

    item.setRegistrationId(mRegistationId.equals("") ?
            GCMIntentService.getRegistrationId() : mRegistationId);
    mTextNewToDo.setText("");
    }
    catch(Exception ex)
    {

    }
}

            public void checkItem(ToDoItem item) {
    if (mClient == null) {
        return;
    }

    // Set the item as completed and update it in the table
    item.setComplete(true);

    mToDoTable.update(item, new TableOperationCallback<ToDoItem>() {

        public void onCompleted(ToDoItem entity, Exception exception, ServiceFilterResponse response) {
            if (exception == null) {
                if (entity.isComplete()) {
                    mAdapter.remove(entity);
                }
            } else {
                createAndShowDialog(exception, "Error");
            }
        }

    });
}

            private void refreshItemsFromTable() {

    // Get the items that weren't marked as completed and add them in the
    // adapter
    mToDoTable.where().field("complete").eq(val(false)).execute(new TableQueryCallback<ToDoItem>() {

        public void onCompleted(List<ToDoItem> result, int count, Exception exception, ServiceFilterResponse response) {
            if (exception == null) {
                mAdapter.clear();

                for (ToDoItem item : result) {
                    mAdapter.add(item);
                }

            } else {
                createAndShowDialog(exception, "Error");
            }
        }
    });
}


private void createAndShowDialog(Exception exception, String title) {
    createAndShowDialog(exception.toString(), title);
}



private void createAndShowDialog(String message, String title) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage(message);
    builder.setTitle(title);
    builder.create().show();
}



             private class ProgressFilter implements ServiceFilter {

    @Override
    public void handleRequest(ServiceFilterRequest request, NextServiceFilterCallback nextServiceFilterCallback,
            final ServiceFilterResponseCallback responseCallback) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (mProgressBar != null) mProgressBar.setVisibility(ProgressBar.VISIBLE);
            }
        });

        nextServiceFilterCallback.onNext(request, new ServiceFilterResponseCallback() {

            @Override
            public void onResponse(ServiceFilterResponse response, Exception exception) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (mProgressBar != null) mProgressBar.setVisibility(ProgressBar.GONE);
                    }
                });

                if (responseCallback != null)  responseCallback.onResponse(response, exception);
            }
        });
    }
}

弄清楚該教程使用的是“而不是”,在我的情況下是問題所在。問題出在蔚藍的插入腳本中。希望對您有所幫助:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM