问题描述
我写了一个简单的Java程序(JDK 1.7),列出了所有我的服务总线主题,并打印出每个主题到标准输出的名称:
I wrote a simply java program (jdk 1.7) that lists all my service bus topics and prints out the name of each topic to stdout:
try {
String namespace = "myservicebus"; // from azure portal
String issuer = "owner"; // from azure portal
String key = "asdjklasdjklasdjklasdjklasdjk"; // from azure portal
Configuration config = ServiceBusConfiguration.configureWithWrapAuthentication(
namespace,
issuer,
key,
".servicebus.windows.net",
"-sb.accesscontrol.windows.net/WRAPv0.9");
ServiceBusContract service = ServiceBusService.create(config);
ListTopicsResult result = service.listTopics();
List<TopicInfo> infoList = result.getItems();
for(TopicInfo info : infoList){
System.out.println( info.getPath());
}
} catch (Exception e) {
e.printStackTrace();
}
现在,我试图在一个简单的Android项目(Android版4.2)运行这个例子,但它不会工作。
运行时总是抛出以下错误:
Now, i am trying to run this example in a simple android project (Android 4.2) but it wont work.The runtime always throws following error:
java.lang.RuntimeException: Service or property not registered: com.microsoft.windowsazure.services.serviceBus.ServiceBusContract
有没有人成功地建立了从Android设备(或仿真器),以蔚蓝的服务总线的连接?
Has anyone successfully established a connection from an android device (or emulator) to azure service bus?
请问微软Azure-Java的SDK不支持Android项目?
Does the Microsoft Azure-Java-SDK not support android projects?
在此先感谢
推荐答案
此错误是由于生成的APK不包括(删除)的ServiceLoader信息(在META-INF /服务)的事实。您可以测试自己从生成的JAR删除它,看看出现同样的错误。在文档据说它现在支持,但我发现问题来使用它。
This error is due to the fact that apks generated do not include (remove) the ServiceLoader information (under META-INF/services). You can test yourself deleting it from the jar generated and see that the same error appears. In the documentation it is said that it is now supported, but i found problems to use it.
您可以通过手动包括在APK数据蚁
You can include manually the data in the apk using ant
10小时调试结束后,手动删除类,包括META-INF /服务等,我发现,在Azure SDK使用不支持Android的一些类(javax.ws。*)和任何woraround为我工作。
After 10h debugging, removing classes manually, including META-INF/services, etc, I found that the Azure SDK uses some classes not supported by Android (javax.ws.*) and any woraround works for me.
因此,我建议使用的Android环境中REST API,找到下面的源$ C $ C I用于sebd消息的话题。
So I would recommend using the REST API in Android environments, find below the source code i used to sebd messages to the topic.
private static String generateSasToken(URI uri) {
String targetUri;
try {
targetUri = URLEncoder
.encode(uri.toString().toLowerCase(), "UTF-8")
.toLowerCase();
long expiresOnDate = System.currentTimeMillis();
int expiresInMins = 20; // 1 hour
expiresOnDate += expiresInMins * 60 * 1000;
long expires = expiresOnDate / 1000;
String toSign = targetUri + "\n" + expires;
// Get an hmac_sha1 key from the raw key bytes
byte[] keyBytes = sasKey.getBytes("UTF-8");
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256");
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(toSign.getBytes("UTF-8"));
// using Apache commons codec for base64
// String signature = URLEncoder.encode(
// Base64.encodeBase64String(rawHmac), "UTF-8");
String rawHmacStr = new String(Base64.encodeBase64(rawHmac, false),"UTF-8");
String signature = URLEncoder.encode(rawHmacStr, "UTF-8");
// construct authorization string
String token = "SharedAccessSignature sr=" + targetUri + "&sig="
+ signature + "&se=" + expires + "&skn=" + sasKeyName;
return token;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void Send(String topic, String subscription, String msgToSend) throws Exception {
String url = uri+topic+"/messages";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
// Add header
String token = generateSasToken(new URI(uri));
post.setHeader("Authorization", token);
post.setHeader("Content-Type", "text/plain");
post.setHeader(subscription, subscription);
StringEntity input = new StringEntity(msgToSend);
post.setEntity(input);
System.out.println("Llamando al post");
HttpResponse response = client.execute(post);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() != 201)
throw new Exception(response.getStatusLine().getReasonPhrase());
}
在REST API Azure的信息进一步的信息。
Further info at REST API Azure information.
这篇关于连接的Azure服务总线与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!