Android获取onenet数据
使用Android获取onenet数据和Java类似,只需要创建线程,加入代码即可
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58btn_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); } }); } });
如果想实时刷新显示数据就每隔几秒请求一次,获取最新数据
复制代码
1
2
3
4
5
6
7
8
9
10
11
12Timer 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秒,再次执行
最后
以上就是雪白羊最近收集整理的关于Android获取onenet数据并实现实时刷新显示--笔记三的全部内容,更多相关Android获取onenet数据并实现实时刷新显示--笔记三内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复