当前位置: 移动技术网 > IT编程>移动开发>Android > Android平台接入OneNET

Android平台接入OneNET

2020年03月18日  | 移动技术网IT编程  | 我要评论

魔法禁书目录 小说,白河优菜,遍地狼烟演员表

1. onenet简介

中国移动物联网开放平台是由中国移动打造的paas物联网开放平台。
平台能够帮助开发者轻松实现设备接入与设备连接,提供综合性的物联网解决方案,实现物联网设备的数据获取,数据存储,数据展现。

中移物联网官方网址

https://open.iot.10086.cn/

安卓平台接入onenet方法:

①注册一个中移物联网的账号

②接下来开始创建产品与产品下的设备

③点击右上角新建产品

④接下来开始创建设备,点击提交之后出现

记住设备id,之后程序里会用到

为设备添加apikey,这也是之后程序里要用到的

保存设备apikey

接下来为设备创建数据流,所有的数据都是被上传到数据流的
这里进行查看数据流

这里添加数据流

接下来就是程序方面的实现
使用http协议,具体的协议内容可以查看

https://open.iot.10086.cn/doc/art/id/190#43

具体二进制文件的传输看这个网址
https://open.iot.10086.cn/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 {
            //data1代表云端的数据流是data1
            string s1 = new string(",;" + "data1" + "," + "哈哈哈,终于成功了");
            byte[] data = s1.getbytes();
            // 先new出一个url对象,传入网络地址
            // 调用openconnection()方法获取到httpurlconnection对象
            // 自己创建的中移物联网的地址http://api.heclouds.com/devices/25857699/datapoints?type=5
            url = new url("http://api.heclouds.com/devices/" + deviceid
                    + "/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.com/devices/25857699/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.com/devices/" + deviceid
                    + "/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();
        }

    }
}

了解更多技术文章,欢迎关注我的个人公众号

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网