Android获取OneNET云平台数据

尝试HttpURLConnection “get”方式获取了www.baidu.com的数据后,试着获取OneNET云平台的设备数据(设备数据已成功上传至云平台)

.java文件

 1 package com.example.onenet_block_get;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 import android.os.Handler;
 7 import android.os.Message;
 8 import android.util.Log;
 9 import android.view.Menu;
10 import android.view.View;
11 import android.widget.Button;
12 import android.widget.TextView;
13 
14 import java.io.BufferedReader;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.net.HttpURLConnection;
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 
22 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
23 
24     TextView responseText;
25     private static  final  String DeviceID = " ";/*填入自己的设备ID*/
26     private static final String Apikey = " ";/*填入自己的Apikey*/
27 
28     @Override
29     protected void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(R.layout.testget);
32         Button sendRequest = (Button) findViewById(R.id.send_request);
33         responseText = (TextView) findViewById(R.id.reponse_text);
34         sendRequest.setOnClickListener(this);
35     }
36 
37     public void onClick(View v) {
38         if (v.getId() == R.id.send_request) {
39             sendRequestWithHttpURLConnection();
40         }
41     }
42 
43     private void sendRequestWithHttpURLConnection() {
44         new Thread(new Runnable() {
45             @Override
46             public void run() {
47                 HttpURLConnection connection = null;
48                 BufferedReader reader = null;
49                 try {
50                     URL url = new URL("https://api.heclouds.com/devices/ ");/*填入自己的设备ID*/
51                     connection = (HttpURLConnection) url.openConnection();
52                     connection.setRequestMethod("GET");
53                     connection.setConnectTimeout(8000);
54                     connection.setReadTimeout(8000);
55                     connection.setRequestProperty("api-key",Apikey);
56                     InputStream in = connection.getInputStream();
57 
58                     reader =  new BufferedReader(new InputStreamReader(in));
59                     StringBuilder response = new StringBuilder();
60                     String line;
61                     while ((line = reader.readLine()) !=null ) {
62                         response.append(line);
63                     }
64                     showResponse(response.toString());
65                 }catch (Exception e){
66                     e.printStackTrace();
67                 }finally {
68                     if(reader != null) {
69                         try {
70                             reader.close();
71                         }catch (IOException e) {
72                             e.printStackTrace();
73                         }
74                     }
75                     if(connection != null) {
76                         connection.disconnect();
77                     }
78                 }
79             }
80         }).start();
81     }
82 
83     private void showResponse(final String response) {
84         runOnUiThread(new Runnable() {
85             @Override
86             public void run() {
87                 responseText.setText(response);
88             }
89         });
90     }
91 }

 

.xml文件
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <Button
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:id="@+id/send_request"
10         android:text="Send Request"/>
11 
12     <ScrollView
13         android:layout_width="match_parent"
14         android:layout_height="match_parent">
15 
16         <TextView
17             android:layout_width="match_parent"
18             android:layout_height="wrap_content"
19             android:id="@+id/reponse_text"/>
20     </ScrollView>
21 </LinearLayout>

下一步就是尝试解析json数据了

 

 
 
 
 
 
 
 
 
 
 

posted on 2020-06-22 14:10  stuMartin  阅读(1907)  评论(0编辑  收藏  举报

导航