首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向CoAP服务器动态添加资源的适当方法

向CoAP服务器动态添加资源的适当方法
EN

Stack Overflow用户
提问于 2016-07-06 16:36:23
回答 1查看 519关注 0票数 0

我创建了一个可以用于向CoAP server.This动态添加URL的方法,这是我目前实现的方法。但是需要知道这是否是将资源添加到CoAP服务器的正确方式?假设coapendpoints/home/room1/sensor1是需要添加的。

代码语言:javascript
复制
    // endpoint--> coapendpoints/home/room1/sensor1
    String[] resources = endpoint.split("/");
    Resource childResource = coapServer.getRoot().getChild(resources[0]);
    Resource parentResource = childResource;
    if (parentResource == null && (resources.length > 1)) {
        coapServer.add(new CoAPResourcePath(resources));
    }
    if (parentResource == null && (resources.length == 1)) {
        coapServer.add(new CoAPResourceServlet(resources[0]));
    } else if (parentResource != null && resources.length > 1) {
        int j = 1;
        for (; j < resources.length; j++) {
            if (childResource != null) {
                parentResource = childResource;
                childResource = childResource.getChild(resources[j]);
            } else {
                break;
            }
        }
        String[] coapEndpoint = Arrays.copyOfRange(resources, j - 1, resources.length);
        if (coapEndpoint.length > 1) {
            parentResource.add(new CoAPResourcePath(coapEndpoint));
        } else if (coapEndpoint.length == 1) {
            parentResource.add(new CoAPResourceServlet(coapEndpoint[0]));
        }
    } else if (parentResource != null && resources.length == 1) {
        parentResource.add(new CoAPResourceServlet(resources[0]));
    }

这里,CoAPResourcePath和CoAPResourceServlet类是从CoapResource扩展而来的。

这是以这种方式添加资源后的输出。

EN

回答 1

Stack Overflow用户

发布于 2017-02-24 13:56:33

我以类似的方式结束了。目前,加利福尼亚没有任何以方便的方式创建嵌套资源的选项。

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

https://stackoverflow.com/questions/38229194

复制
相关文章

相似问题

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