簡體   English   中英

通過Rest API在Android上連接Azure服務總線

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

我正在編寫一個Android應用程序並連接到Azure服務總線-主題。 我編寫了代碼以連接到現有的名稱空間,例如本教程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);

但是它在ServiceBusContract service = ServiceBusService.create(config);處引發異常。 它說:“ java.lang.RuntimeException: Service or property not registered: com.microsoft.windowsazure.services.serviceBus.ServiceBusContract我查找了某種方法來解決它,但還不行。所以我使用REST API連接了本教程的REST API蔚藍

它可以運行並將消息發送到Azure Service Bus-主題。 現在,我想編寫一種用於從該主題的訂閱中獲取消息的方法。 像這樣使用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");

但是它無法獲取消息,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>"

我不知道為什么不支持它。 你能幫助我嗎。 謝謝

根據您在Azure Java SDK中的代碼,此問題是由使用不正確的namespace sasKeyNamesasKey 在參考文檔中,作者創建了命名空間HowToSample和SAS密鑰SAS_key_value ,與您的命名空間不同。

因此,您需要用創建的服務總線的名稱空間和SAS密鑰值替換它們。 您可以在Azure門戶或新門戶上找到它們。

對於Service Bus REST API,請嘗試在Peek-Lock Message API的查詢字符串中添加api-version ,請注意參考文檔https://msdn.microsoft.com/zh-cn/library/中的內容azure / hh780771.aspx

版本和服務器概述

遵循Azure准則,REST API現在支持版本控制。 默認情況下,不傳遞任何版本表示當前服務行為。 現有REST API的任何更改都需要提供api版本值作為查詢字符串。

版本 “ 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