Android平台接入OneNET

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android平台接入OneNET

yz0001   2020-03-18 我要评论
## 1. OneNET简介 中国移动物联网开放平台是由中国移动打造的PaaS物联网开放平台。 平台能够帮助开发者轻松实现设备接入与设备连接,提供综合性的物联网解决方案,实现物联网设备的数据获取,数据存储,数据展现。 中移物联网官方网址 https://open.iot.10086.cn/ ## 安卓平台接入OneNET方法: **①注册一个中移物联网的账号** ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084538594-427772065.png) **②接下来开始创建产品与产品下的设备** ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084539139-1407744757.png) **③点击右上角新建产品** ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084539390-439202722.png) ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084539524-262359424.png) ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084539645-1270990191.png) ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084539778-30397561.png) ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084539915-1407306370.png) **④接下来开始创建设备,点击提交之后出现** ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084540130-1139416458.png) ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084540332-1705416907.png) **记住设备id,之后程序里会用到** ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084540493-960314884.png) **为设备添加APIKey,这也是之后程序里要用到的** ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084540657-1452542764.png) **保存设备APIKey** ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084540827-1537691858.png) **接下来为设备创建数据流,所有的数据都是被上传到数据流的 这里进行查看数据流** ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084540988-1241800910.png) **这里添加数据流** ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084541167-147600607.png) 接下来就是程序方面的实现 使用http协议,具体的协议内容可以查看 https://open.iot.10086.cnhttps://img.qb5200.com/download-x/doc/art/id/190#43 具体二进制文件的传输看这个网址 https://open.iot.10086.cnhttps://img.qb5200.com/download-x/doc/art258.html#68 以下http协议代码的实现都是基于这个规定的,一定要打开对照来看 ### (1) 以POST实现文本数据的上传 ``` public class OneNETUrlConnectionPost extends Thread { private int SHOW_REQUEST = 0; private Handler handler; // 首先声明设备ID及apikey private static final String DeviceID = "25857699"; // 个人使用就建议填产品key,设备key在二进制获取那里会权限不足 private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM="; public OneNETUrlConnectionPost(Handler handler) { this.handler = handler; } @Override public void run() { URL url; HttpURLConnection connection; try { /https://img.qb5200.com/download-x/data1代表云端的数据流是data1 String s1 = new String(",;" + "data1" + "," + "哈哈哈,终于成功了"); byte[] data = s1.getBytes(); // 先new出一个URL对象,传入网络地址 // 调用openConnection()方法获取到HttpURLConnection对象 // 自己创建的中移物联网的地址http://api.heclouds.comhttps://img.qb5200.com/download-x/devices/25857699https://img.qb5200.com/download-x/datapoints?type=5 url = new URL("http://api.heclouds.comhttps://img.qb5200.com/download-x/devices/" + DeviceID + "https://img.qb5200.com/download-x/datapoints?type=5"); connection = (HttpURLConnection) url.openConnection(); // 下面使一些自由的定制,比如设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头 connection.setConnectTimeout(8000); connection.setReadTimeout(8000); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("api-key", ApiKey); connection.setRequestProperty("Content-Length", String.valueOf(data.length)); connection.setChunkedStreamingMode(5); // 设置打开输出流 connection.setDoOutput(true); // 拿到输出流 OutputStream os = connection.getOutputStream(); // 使用输出流往服务器提交数据 os.write(data); os.flush(); os.close(); // //如果请求发送成功 if (connection.getResponseCode() == 200) { // 接下来利用输入流对数据进行读取 InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader( new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = br.readLine()) != null) { response.append(line); }// 正常则返回{"errno":0,"error":"succ"},此函数为void,用不上这个 // 发送数据完毕,接下来用Handler进行提交成功显示 Message message = new Message(); message.what = SHOW_REQUEST; message.obj = response.toString(); handler.sendMessage(message); } // 最后关闭HTTP连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` ### (2) 以POST实现字节数据的上传(包括图片视频等二进制文件) ``` public class OneNETUrlConnectionPostByte extends Thread { private int SHOW_REQUEST = 0; private Handler handler; // 首先声明设备ID及apikey private static final String DeviceID = "25857699"; // 个人使用就建议填产品key,设备key在二进制获取那里会权限不足 private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM="; public OneNETUrlConnectionPostByte(Handler handler) { this.handler = handler; } @Override public void run() { URL url; // 自定义的字符数组将它上传到云端 byte[] my_data = { '8', '8', '6' }; HttpURLConnection connection; try { // 先new出一个URL对象,传入网络地址 // 调用openConnection()方法获取到HttpURLConnection对象 // 自己创建的中移物联网的地址http://api.heclouds.comhttps://img.qb5200.com/download-x/devices/25857699https://img.qb5200.com/download-x/datapoints?type=5 url = new URL("http://api.heclouds.com/bindata?device_id=" + DeviceID + "&datastream_id=" + "data123"); connection = (HttpURLConnection) url.openConnection(); // 下面使一些自由的定制,比如设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头 connection.setConnectTimeout(8000); connection.setReadTimeout(8000); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("api-key", ApiKey); connection.setRequestProperty("Content-Length", String.valueOf(my_data.length)); connection.setChunkedStreamingMode(5); // 设置打开输出流 connection.setDoOutput(true); // 拿到输出流 OutputStream os = connection.getOutputStream(); // 使用输出流往服务器提交数据 os.write(my_data); os.flush(); os.close(); // //如果请求发送成功 if (connection.getResponseCode() == 200) { // 接下来利用输入流对数据进行读取 InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader( new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = br.readLine()) != null) { response.append(line); }// 正常则返回{"errno":0,"error":"succ"},此函数为void,用不上这个 // 发送数据完毕,接下来用Handler进行提交成功显示 Message message = new Message(); message.what = SHOW_REQUEST; message.obj = response.toString(); handler.sendMessage(message); } // 最后关闭HTTP连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` ### (3) 以GET实现文本数据的获取 ``` public class OneNETHttpRequestGET extends Thread { private int SHOW_REQUEST = 0; private Handler handler; // 首先声明设备ID及apikey private static final String DeviceID = "25857699"; // 个人使用就建议填产品key,设备key在二进制获取那里会权限不足 private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM="; public OneNETHttpRequestGET(Handler handler) { this.handler = handler; } @Override public void run() { URL url; HttpURLConnection connection; try { // 先new出一个URL对象,传入网络地址 // 调用openConnection()方法获取到HttpURLConnection对象 url = new URL("http://api.heclouds.comhttps://img.qb5200.com/download-x/devices/" + DeviceID + "https://img.qb5200.com/download-x/datastreams/" + "data1"); connection = (HttpURLConnection) url.openConnection(); // 下面使一些自由的定制,比如设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头 connection.setConnectTimeout(15000); connection.setReadTimeout(15000); connection.setRequestMethod("GET"); connection.setRequestProperty("api-key", ApiKey); // 如果网页正确响应 if (connection.getResponseCode() == 200) { // 接下来利用输入流对数据进行读取 InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader( new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = br.readLine()) != null) { response.append(line); } // 读取数据完毕,接下来将数据传送到Handler进行显示 Message message = new Message(); message.what = SHOW_REQUEST; message.obj = response.toString(); handler.sendMessage(message); // 最后关闭HTTP连接 connection.disconnect(); } } catch (Exception e) { e.printStackTrace(); } } } ``` ### (4) 以GET实现字节数据的获取,但是存在小bug还未成功 ``` public class OneNETHttpRequestGETByte extends Thread { private int SHOW_REQUEST = 0; private Handler handler; // 首先声明设备ID及apikey private static final String DeviceID = "25857699"; // 个人使用就建议填产品key,设备key在二进制获取那里会权限不足 private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM="; public OneNETHttpRequestGETByte(Handler handler) { this.handler = handler; } @Override public void run() { URL url; HttpURLConnection connection; try { // 先new出一个URL对象,传入网络地址 // 调用openConnection()方法获取到HttpURLConnection对象 url = new URL("http://api.heclouds.com/bindata/" + "25857699_15201013688560_data123"); connection = (HttpURLConnection) url.openConnection(); // 下面使一些自由的定制,比如设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头 connection.setConnectTimeout(15000); connection.setReadTimeout(15000); connection.setRequestMethod("GET"); connection.setRequestProperty("api-key", ApiKey); // 如果网页正确响应 if (connection.getResponseCode() == 200) { // 接下来利用输入流对数据进行读取 InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader( new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = br.readLine()) != null) { response.append(line); } // 读取数据完毕,接下来将数据传送到Handler进行显示 Message message = new Message(); message.what = SHOW_REQUEST; message.obj = response.toString(); handler.sendMessage(message); // 最后关闭HTTP连接 connection.disconnect(); } } catch (Exception e) { e.printStackTrace(); } } } ``` 了解更多技术文章,欢迎关注我的个人公众号 ![](https://img2020.cnblogs.com/other/1617275/202003/1617275-20200318084541319-1647372901.jpg)

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们