首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure Service Bus与Android的连接

Azure Service Bus与Android的连接
EN

Stack Overflow用户
提问于 2013-03-09 21:10:58
回答 1查看 1.6K关注 0票数 5

我编写了一个简单的java程序(jdk 1.7),它列出了我的所有服务总线主题,并将每个主题的名称打印到stdout:

代码语言:javascript
复制
         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)中运行这个示例,但它不起作用。运行时总是抛出以下错误:

代码语言:javascript
复制
java.lang.RuntimeException: Service or property not registered:  com.microsoft.windowsazure.services.serviceBus.ServiceBusContract

有人成功地建立了从android设备(或模拟器)到azure服务总线的连接吗?

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

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2015-06-15 20:08:59

此错误是由于生成的apks不包含(删除)服务信息(在META-INF/ ServiceLoader下)造成的。您可以对自己进行测试,将其从生成的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.*),任何works都适合我。

因此,我建议在Android环境中使用REST API,可以在下面找到我用来将消息发送到主题的源代码。

代码语言:javascript
复制
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());

}

静态API Azure信息的更多信息。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15311060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档