如何运用REST API接口用Java向Vantiq平台发送信息

我们知道在Vantiq平台中的每一个资源都是一个service。Everything is a service。这句话的含义就是,我们可以通过REST接口来访问Vantiq平台的所有的资源,这包括创建,修改,删除这些资源。这些资源包括type(数据表格),topic,procedure的调用。我们可以通过Vantiq平台提供的JAVA SDK或Nodejs SDK来访问这些资源,甚至我们可以使用Micro Edition来在微处理器中来访问Vantiq平台所提供的资源。我们甚至提供了iOS SDK工开发者来开发。

在今天的这篇文章中,来讲述如果使用Java语言来访问Vantiq的一些资源。虽然,这里的Vantiq提供了专门的Java SDK,我们可以直接使用Vantiq平台提供的REST接口来访问。

在Vantiq平台的type中插入一个记录

我们可以使用如下的代码来实现一个记录的插入:

	CloseableHttpClient client = HttpClients.createDefault();
	
	HttpPost post = new HttpPost("https://dev.vantiq.cn/api/v1/resources/custom/Person");
	
	JSONObject json = new JSONObject();
	json.put("id", "5");
	json.put("name", "yyyyyyy");
	json.put("number", "00000000000");
	
	String body = json.toString();
	
	System.out.println("body: " + body);
	HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
	post.setEntity(entity);
	
	post.setHeader("Content-Type", "application/json; charset=UTF-8");
	post.addHeader(new BasicHeader("Authorization", "Bearer Your token in your namespace"));
	
	CloseableHttpResponse respnse = client.execute(post);
	
	System.out.println(respnse.getStatusLine().getStatusCode()); 

在我们实现时,我们需要记得修个这里的如下行:

一旦修改完成,运行上面的代码,我们可以看到:
在这里插入图片描述

我们可以看到在Person这个type里有一个新的记录产生。

调用Vantiq平台的procedure接口

同样的方法,我们可以使用Vantiq平台提供的REST API即可来访问在Vantiq平台里定义的一个procedure,并把相应的参数传入到这个procedure里去。在如下的代码中,我们调用一个叫做“rest_procedure”的procedure。

   	class JsonObjectBuilder {

        private JsonObject obj = new JsonObject();

        public JsonObjectBuilder addProperty(String name, String value) {
            this.obj.addProperty(name, value);
            return this;
        }

        public JsonObjectBuilder addProperty(String name, Number value) {
            this.obj.addProperty(name, value);
            return this;
        }

        public JsonObject obj() {
            return this.obj;
        }

        public String json() {
        	Gson gson = new Gson();
            return gson.toJson(this.obj);
        }
    }
        
    JsonObject msg3 = new JsonObjectBuilder().addProperty("machineId", "Machine2").obj();
    msg3.addProperty("temperature", 333);
    msg3.addProperty("timestamp", "2019-01-04T09:05:58.004Z");
	
	
	CloseableHttpClient client = HttpClients.createDefault();
	
	HttpPost post = new HttpPost("https://dev.vantiq.cn/api/v1/resources/procedures/rest_procedure");
	// /api/v1/resources/procedures/ConvertToCelcius
	
	JSONObject json = new JSONObject();
	json.put("msg", msg3);
	
	String body = json.toString();
	
	System.out.println("body: " + body);
	HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
	post.setEntity(entity);
	
	post.setHeader("Content-Type", "application/json; charset=UTF-8");
	post.addHeader(new BasicHeader("Authorization", "Bearer Your token your namespace"));
	
	CloseableHttpResponse respnse = client.execute(post);
	
	System.out.println(respnse.getStatusLine().getStatusCode()); 
	System.out.println("response: " + respnse);

同样,我们需要对如下的一行代码进行修改:

运行我们的代码,我们可以看到: 在这里插入图片描述

当我们运行完我们的代码,我们可以看到我们的procedure被调用,并把相应的参数传入到Vantiq平台中去了。

向Vantiq平台的topic发送数据

很多的时候,我们可以利用Vantiq平台提供的REST API接口向Vantiq里的topic发布信息,并把相应的消息传入到平台中。在如下的代码中,它展示了想Vantiq平台中的/xiaoguo/publish_topic发送消息。

	class JsonObjectBuilder {

        private JsonObject obj = new JsonObject();

        public JsonObjectBuilder addProperty(String name, String value) {
            this.obj.addProperty(name, value);
            return this;
        }

        public JsonObjectBuilder addProperty(String name, Number value) {
            this.obj.addProperty(name, value);
            return this;
        }

        public JsonObject obj() {
            return this.obj;
        }

        public String json() {
        	Gson gson = new Gson();
            return gson.toJson(this.obj);
        }

    }
        
    JsonObject msg3 = new JsonObjectBuilder().addProperty("machineId", "Machine2").obj();
    msg3.addProperty("temperature", 333);
    msg3.addProperty("timestamp", "2019-01-04T09:05:58.004Z");
	
	
	CloseableHttpClient client = HttpClients.createDefault();
	
	HttpPost post = new HttpPost("https://dev.vantiq.cn/api/v1/resources/topics//xiaoguo/publish_topic");
	
	JSONObject json = new JSONObject();
	json.put("msg", msg3);
	
	String body = json.toString();
	
	System.out.println("body: " + body);
	HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
	post.setEntity(entity);
	
	post.setHeader("Content-Type", "application/json; charset=UTF-8");
	post.addHeader(new BasicHeader("Authorization", "Bearer Your token in your namespace"));
	
	CloseableHttpResponse respnse = client.execute(post);
	
	System.out.println(respnse.getStatusLine().getStatusCode()); 
	System.out.println("response: " + respnse);

同样的,我们需要修改如下的这一行代码:

当我们运行完后,我可以看到:

在这里插入图片描述

我们可以看出来,我们发布的消息已经发布到Vantiq平台了。

这个项目的源码可以在如下的地址获得:

https://github.com/liu-xiao-guo/vantiq-java-REST-example

作者:UbuntuCore及Phone
来源:CSDN
原文:https://blog.csdn.net/UbuntuTouch/article/details/89154543
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值