当前位置: 首页 > news >正文

丰县建设局规划局网站注册网站的信息网站

丰县建设局规划局网站,注册网站的信息网站,做地方门户网站赚钱吗,做百度移动网站点击目录 前言 一、CoapClient对象 1、类定义 2、Client方法调用 二、发送请求 1、构建请求 2、发起请求 3、接收响应 总结 前言 在之前的博客中物联网协议Coap之Californium CoapServer解析#xff0c;文中简单介绍了CoapServer的实现。在物联网开发环境中#xff0c;除了… 目录 前言 一、CoapClient对象 1、类定义 2、Client方法调用 二、发送请求 1、构建请求 2、发起请求 3、接收响应 总结 前言 在之前的博客中物联网协议Coap之Californium CoapServer解析文中简单介绍了CoapServer的实现。在物联网开发环境中除了Server端需要定义很多的开发场景是在客户端的开发这涉及设备端的交互比如传感器的数据采集需要通过Client的put方法进行采集数据的提交同时通过get方法获取服务器端的指令然后在Client端进行采集。 本次我们简单来看看CoapClient的具体实现博文将继续采用面向对象分析的方法结合类图、实际代码、时序图来讲讲解CoapClient类方便了解和掌握其相关的配置同时掌握其运行原理。在实际的终端开发中有的放矢。行文仓促定有不当之处欢迎各位读者批评指正再此感谢。 一、CoapClient对象 在Coap的世界中并不是像http协议一样只要是浏览器就能发http请求Coap需要实现对应的CoapClient以此来跟Server建立通讯实现数数据的提交服务的交互。 1、类定义 在CoapClient的构造方法中有三种构造的方式 /*** Constructs a new CoapClient that sends requests to the specified URI.** param uri the uri*/public CoapClient(String uri) {this.uri uri;}/*** Constructs a new CoapClient that sends request to the specified URI.* * param uri the uri*/public CoapClient(URI uri) {this(uri.toString());}/*** Constructs a new CoapClient with the specified scheme, host, port and* path as URI.** param scheme the scheme* param host the host* param port the port* param path the path*/public CoapClient(String scheme, String host, int port, String... path) {StringBuilder builder new StringBuilder().append(scheme).append(://).append(host).append(:).append(port);for (String element : path) {builder.append(/).append(element);}this.uri builder.toString();} 2、Client方法调用 在CoapClient中定义了包括get、put、delete、post等方法的定义在这里只是进行入口函数的编写。下节将重点讲解在CoapClient中如何进行相应请求的发送。 // Asynchronous GET/*** Sends a GET request and invokes the specified handler when a response* arrives.** param handler the Response handler*/public void get(CoapHandler handler) {asynchronous(newGet().setURI(uri), handler);}/*** Sends aGET request with the specified Accept option and invokes the* handler when a response arrives.* * param handler the Response handler* param accept the Accept option*/public void get(CoapHandler handler, int accept) {asynchronous(accept(newGet().setURI(uri), accept), handler);}// Synchronous POST/*** Sends a POST request with the specified payload, the specified content* format and accept and invokes the specified handler when a response* arrives.* * param handler the Response handler* param payload the payload* param format the Content-Format* param accept the Accept option*/public void post(CoapHandler handler, byte[] payload, int format, int accept) {asynchronous(accept(format(newPost().setURI(uri).setPayload(payload), format), accept), handler);}/*** Sends a PUT request with the specified payload and the specified content* format and invokes the specified handler when a response arrives.* * param handler the Response handler* param payload the payload* param format the Content-Format*/public void put(CoapHandler handler, byte[] payload, int format) {asynchronous(format(newPut().setURI(uri).setPayload(payload), format), handler);}/*** Sends a DELETE request and invokes the specified handler when a response* arrives.** param handler the response handler*/public void delete(CoapHandler handler) {asynchronous(newDelete().setURI(uri), handler);} 二、发送请求 在构建好CoapClient对象后就可以往目标服务器提交请求并获取响应结果了。这里详细讲解在Coap中如何进行请求的发送。下面是之前创建CoapClient以及发送get请求的关键代码 URI uri null; //coap://127.0.0.1:5683/core/time?type1 uri new URI(coap://localhost:5683/hello); // 创建一个资源请求hello资源注意默认端口为5683 //uri new URI(coap://127.0.0.1:5683/core/time?type1); CoapClient client new CoapClient(uri); CoapResponse response client.get(); 1、构建请求 通过代码跟踪和时序图以发送get请求为例来看看底层究竟是怎么运行的。 第一步在调用get()方法时进入以下函数 第二步进入核心的请求函数 默认情况下我们没有给请求设置超时时间因此它会根据配置文件加载默认的超时时间。然后根据请求方式和携带的参数都封装到request对象中。 这里可以看到get请求携带的参数如下 CON-GET MID -1, Tokennull, OptionSet{Uri-Host:localhost, Uri-Path:hello}, no payload 2、发起请求 在这里通过endPoint对象来进行发送。org.eclipse.californium.core.network.CoapEndpoint中的sendRequest方法。最终的请求参数如下 Overridepublic void sendRequest(final Request request) {// create context, if not already setrequest.prepareDestinationContext();// always use endpoint executorrunInProtocolStage(new Runnable() {Overridepublic void run() {coapstack.sendRequest(request);}});} 可以看到在发送请求的时候是开启了一个线程池来进行请求发送。 在org.eclipse.californium.core.network.stack.BaseCoapStack 3、接收响应 ACK-2.05 MID20132, Token[643cec40ed6f22c6], OptionSet{Content-Format:text/plain}, Hello CoAP!This is from .. 40 bytes 可以看到通过response对象就可以正常获取从服务端返回的响应信息。 总结 以上就是本文的主要内容本文将继续采用面向对象分析的方法结合类图、实际代码、时序图来讲讲解CoapClient类方便了解和掌握其相关的配置同时掌握其运行原理。在实际的终端开发中有的放矢。
http://www.zqtcl.cn/news/185286/

相关文章:

  • 天元建设集团有限公司破产新手seo网站做什么类型好
  • spa.net网站开发二次开发需要什么
  • 如何做网站静态页面商丘网签查询
  • 网站建设好学么模版型网站是怎样的
  • 网站维护建设费应计入科目高端营销型网站制作
  • 推荐几个好的网站wordpress 加载数据库表格也卖弄
  • 承德网站开发找人做网站安全吗
  • 百度网站推广电话眼镜网站怎么做竞价
  • 邢台建设银行官方网站为什么建设网站很多公司没有
  • 闵行做网站费用湖南正规网络营销哪家便宜
  • 找个公司做网站需要注意什么wordpress用户名长度
  • 推荐几个没封的正能量网站营销技巧和营销方法视频
  • html mip 网站桂林市临桂区
  • 做网站如何月入10万建行app怎么注册登录
  • 建设一个旅游网站毕业设计建设网站的功能定位是什么原因
  • wordpress网站导航模板杭州建设网站的公司
  • 如何做视频解析网站wordpress 关闭评论
  • 安福网站建设微信开发者工具怎么下载
  • 网罗设计网站威海网页设计制作公司
  • 网站用cmswordpress插件怎么做
  • 如何办好公司网站元器件网站搭建
  • 建设领域行政处罚查询网站wordpress数据库发文章
  • 怎么做网页的多开器宿迁seo优化
  • 别人帮做的网站怎么修改病句店铺引流的30种方法
  • 网站备案幕布怎么申请绍兴cms建站模板
  • 做网站熊掌号软件设计公司排名
  • 深圳 做网站学做西点的网站
  • 静态网站安全性百度服务平台
  • 网站vi设计公司网站建设app
  • 书店网站建设策划书总结每天看七个广告赚40元的app