Windows Azure网站与Android设备通信

时间:2013-03-13 06:18:54

标签: android azure

项目要求:使用天蓝色移动服务在windows azure网站和Android设备之间进行通信

1)网站使用Microsoft Windows Azure(服务器端)在VB.NET中构建

2)Android应用程序位于Android设备(客户端)

客户端(Android用户)应该能够将数据从Android设备(客户端)发送到网站(服务器端),反之亦然。为了实现这一点,即客户端和服务器之间的通信我正在使用Microsoft提供的移动服务使用GCM(Google Cloud Messaging)的Windows Azure(服务器端)

我已根据文档

执行了所有步骤

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

还按照上面Microsoft windows azure链接文档中提供的所有步骤进行操作

但是当我尝试从Android设备向网站发送消息时,会发生以下错误

错误: com.microsoft.windowsazure.mobileservices.MobileServiceException:处理请求时出错

注意:GCM(Google Cloud Messaging)为我们提供了一个gcm.jar文件,该文件在Android应用程序中用于向服务器发送数据,即网站

ONCREATE CODE

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

On BUTTON点击事件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);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

想出教程使用'而不是',在我的例子中是问题。问题出在azure上的插入脚本中。希望它可以帮助你:-)