简体   繁体   English

通过Rest API在Android上连接Azure服务总线

[英]Connect Azure service bus on Android by Rest API

I'm writting an Android App and connect to Azure Service bus - Topic. 我正在编写一个Android应用程序并连接到Azure服务总线-主题。 I write code to connect to existing namespace like this tutorial https://azure.microsoft.com/en-us/documentation/articles/service-bus-java-how-to-use-topics-subscriptions/ 我编写了代码以连接到现有的名称空间,例如本教程https://azure.microsoft.com/zh-cn/documentation/articles/service-bus-java-how-to-use-topics-subscriptions/

Configuration config = ServiceBusConfiguration.configureWithSASAuthentication(
  "HowToSample",
  "RootManageSharedAccessKey",
  "SAS_key_value",
  ".servicebus.windows.net"
  );

ServiceBusContract service = ServiceBusService.create(config);

But it throw an exception at ServiceBusContract service = ServiceBusService.create(config);. 但是它在ServiceBusContract service = ServiceBusService.create(config);处引发异常。 It said " java.lang.RuntimeException: Service or property not registered: com.microsoft.windowsazure.services.serviceBus.ServiceBusContract I looked up some way to fix it but not ok. So i use REST API to connect same this tutorial REST API AZURE 它说:“ java.lang.RuntimeException: Service or property not registered: com.microsoft.windowsazure.services.serviceBus.ServiceBusContract我查找了某种方法来解决它,但还不行。所以我使用REST API连接了本教程的REST API蔚蓝

It can run and sent message to Azure Service Bus - Topic. 它可以运行并将消息发送到Azure Service Bus-主题。 Now i want to write a method use to get message from this Topic's Subcription. 现在,我想编写一种用于从该主题的订阅中获取消息的方法。 Use Rest API like that 像这样使用Rest API

String url = "https://smarthomethesis.servicebus.windows.net/light_1/Subscriptions/LightSubscription/messages/head?timeout=60";

HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);

// Add header
String token = generateSasToken(new URI(uri));

get.setHeader("Authorization", token);
System.out.println("Llamando al post");
HttpResponse response = client.execute(get);

HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");

But it can't get message, responseString show 但是它无法获取消息,responseString显示

"<Error><Code>400</Code><Detail>The request is not supported for the supplied api-version ''. TrackingId:d7624c10-4fc9-43d6-b092-880947e3b820_G31,TimeStamp:1/10/2016 3:43:44 PM</Detail></Error>"

I don't know why it isn't supported. 我不知道为什么不支持它。 Can you help me. 你能帮助我吗。 Thank you 谢谢

According to your code in Azure Java SDK, the issue was caused by using incorrect namespace , sasKeyName & sasKey . 根据您在Azure Java SDK中的代码,此问题是由使用不正确的namespace sasKeyNamesasKey In the reference doc, the namespace HowToSample & SAS key SAS_key_value were created by the author, that be different from yours. 在参考文档中,作者创建了命名空间HowToSample和SAS密钥SAS_key_value ,与您的命名空间不同。

So you need to replace them with the namespace & SAS key value of your service bus created. 因此,您需要用创建的服务总线的名称空间和SAS密钥值替换它们。 You can find them on Azure portal or new portal. 您可以在Azure门户或新门户上找到它们。

For the Service Bus REST API, try to add the api-version in the query string for the Peek-Lock Message API, please note the content from the reference doc https://msdn.microsoft.com/en-us/library/azure/hh780771.aspx . 对于Service Bus REST API,请尝试在Peek-Lock Message API的查询字符串中添加api-version ,请注意参考文档https://msdn.microsoft.com/zh-cn/library/中的内容azure / hh780771.aspx

Version and server overview 版本和服务器概述

Following Azure guidelines, REST APIs now support versioning. 遵循Azure准则,REST API现在支持版本控制。 By default, passing no version indicates current service behavior. 默认情况下,不传递任何版本表示当前服务行为。 Any changes in existing REST APIs require providing an api-version value as the query string. 现有REST API的任何更改都需要提供api版本值作为查询字符串。

Version “2012-03”,“2012-08”,“2013-04”,“2013-07”,“2013-08”,“2013-10”,“2014-01”,“2015-01” 版本 “ 2012-03”,“ 2012-08”,“ 2013-04”,“ 2013-07”,“ 2013-08”,“ 2013-10”,“ 2014-01”,“ 2015-01”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM