Android获取onenet数据并实现实时刷新显示--笔记三

Android获取onenet数据
使用Android获取onenet数据和Java类似,只需要创建线程,加入代码即可

btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    sendre();
            }
            private void sendre(){
                //开启线程,发送请求
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpURLConnection connection=null;
                        BufferedReader reader=null;
                        try {
                            URL url=new URL("http://api.heclouds.com/devices/722413660/datastreams/humidity");
                            connection = (HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("GET");
                            connection.setConnectTimeout(10000);
                            connection.setReadTimeout(10000);
                            //设置格式
                            connection.setRequestProperty("Content-type", "application/json");
                            //设置验证方式
                            connection.setRequestProperty("authorization", "version=2018-10-31&res=products%2F430138&et=1672735919&method=md5&sign=RTtOqY83oqgwlSYU5DPiQA%3D%3D");
                            InputStream in = connection.getInputStream();
                            reader = new BufferedReader(new InputStreamReader(in));
                            StringBuilder response = new StringBuilder();
                            String line;
                            while ((line = reader.readLine()) != null) {
                                response.append(line);
                            }
                            show(response.toString());
                        } catch (MalformedURLException | ProtocolException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }finally {
                            if (connection != null) {
                                connection.disconnect();
                            }
                        }
                    }
                }).start();
            }
            private void show(final String response){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //显示数据到UI控件上
                        String result=response.toString();
                        JSONObject jsonObject = JSON.parseObject(result);
                        JSONObject jsonObject1=jsonObject.getJSONObject("data");
                        String b=jsonObject1.getString("current_value");
                        text_3.setText("当前湿度:"+b+"%");
                        text_2.setText(response);
                    }
                });
            }
        });

在这里插入图片描述
如果想实时刷新显示数据就每隔几秒请求一次,获取最新数据

	Timer timer=new Timer(true);
        TimerTask task = new TimerTask(){
            @Override
            public void run() {
            //your code
            
                sendre();
                //System.out.println("11222");
            }
        };
        timer.schedule(task, 1000, 2000);//等待1秒后,执行一次,之后每隔2秒,再次执行
  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值