将Azure Service Bus与Android连接

将Azure Service Bus与Android连接,第1张

概述我写了一个简单的 java程序(jdk 1.7),它列出了我所有的服务总线主题,并将每个主题的名称打印到stdout: try { String namespace = "myservicebus"; // from azure portal String issuer = "owner"; // from azure portal Str 我写了一个简单的 java程序(jdk 1.7),它列出了我所有的服务总线主题,并将每个主题的名称打印到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)中运行此示例,但它不会工作.
运行时总是抛出以下错误:

@R_419_4126@.RuntimeException: Service or property not registered:  com.microsoft.windowsazure.services.serviceBus.ServiceBusContract

有没有人成功建立从AndroID设备(或模拟器)到天蓝色服务总线的连接

Microsoft Azure-Java-SDK不支持AndroID项目吗?

提前致谢

解决方法 此错误是由于生成的apks不包含(删除)ServiceLoader信息(在meta-inf / services下).您可以测试自己从生成的jar中删除它,并看到出现相同的错误.在文档中,据说现在支持它,但我发现使用它有问题.

http://developer.android.com/reference/java/util/ServiceLoader.html

您可以使用ant手动包含apk中的数据

Keep ‘META-INF/services’-files in apk

经过10小时的调试,手动删除类,包括meta-inf / services等,我发现Azure SDK使用了AndroID不支持的一些类(javax.ws.*)和任何类似的东西.

所以我建议在AndroID环境中使用REST API,在下面找到我用来sebd消息的源代码.

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信息的更多信息.

总结

以上是内存溢出为你收集整理的将Azure Service Bus与Android连接全部内容,希望文章能够帮你解决将Azure Service Bus与Android连接所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1126884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存