项目需求:windows azure网站和android设备使用azure移动服务进行通信
1)网站是在VB.NET中使用Microsoft Windows Azure(服务器端)构建的
2)Android应用程序在Android设备(客户端)
客户端(Android 用户)应该能够将数据从 android 设备(客户端)发送到网站(服务器端),反之亦然为了使这成为可能,即客户端和服务器之间的通信我正在使用 Microsoft Windows Azure 提供的移动服务(服务器端)使用 GCM(谷歌云消息)
我已按照文档执行所有步骤
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 应用程序中用于向服务器即网站发送数据
创建代码
@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);
}
});
}
}