获取新大陆云平台传感器数据 Android java

获取的是下边这个数据,其他的没有研究
在这里插入图片描述
首先获取token
在这里插入图片描述

保存好右侧的token
先看android java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
        button.setOnClickListener(v -> {
//            String s = getNleCloudUtils.sendRequest(url);
            String s = getNleCloudUtils.sendRequest();
            Log.e("TAG", "onCreate: " + s);
            textView.setText(s);
        });
    }

这里没有使用第三方插件,都是自带的一些库

public class getNleCloudUtils {
    private static final String token =
            "上一步获取到的token";


    //    public void sendCommand(String devIds, String accessToken) {//数据
//        new Thread(() -> {
//            OkHttpClient ok = new OkHttpClient();
//            MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
//            Request request = new Request.Builder()
//                    .url("https://api.nlecloud.com/Devices/Datas?devIds=" + devIds)
//                    .get()
//                    .header("AccessToken", accessToken)
//                    .build();
//            try {
//                Response response = ok.newCall(request).execute();
//                String body = response.body().string();
//                Log.e("TAG", body);
//                getNleCloudUtils.parseJSONWithJSONObject(body);
//            } catch (IOException e) {
//                e.printStackTrace();
//                Log.e("TAG", "zxq_error:" + e.getMessage());
//            }
//        }).start();
//    }
    public static String sendRequest() {


        MyCallable1 myCallable1 = new MyCallable1();
        FutureTask<String> ft = new FutureTask<>(myCallable1);
        new Thread(ft).start();
        // 可能做一些其他操作
        try {
//            System.out.println("ru:" + ft.get());
            Log.e("TAG", "sendRequest: " + ft.get());
            return ft.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String parseJSONWithJSONObject(String jsonData) {
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            JSONArray resultObj = jsonObject.getJSONArray("ResultObj");
            //通过循环获取数据,并放入list集合中
            String datas = resultObj.getJSONObject(0).getString("Datas");
            JSONArray jsonArray = new JSONArray(datas);
            return jsonArray.getJSONObject(0).getString("Value");
        } catch (
                Exception e) {
            e.printStackTrace();
            Log.e("TAG", "parseJSONWithJSONObject: error" + e.getMessage());
        }
        return "";
    }
}

class MyCallable1 implements Callable<String> {
    // Callable的任务执行后可返回值,而Runnable的任务是不能返回
    @Override
    public String call() throws Exception {
        try {
            // 线程安全
            StringBuffer resultBuffer;
            HttpURLConnection con = null;
            // 字符输入流
            BufferedReader buffer = null;

            URL url = new URL("https://api.nlecloud.com/Devices/Datas?devIds=535123");
            //得到连接对象
            con = (HttpURLConnection) url.openConnection();
            //设置请求类型
            con.setRequestMethod("GET");
            //设置请求需要返回的数据类型和字符集类型
            con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

            con.setRequestProperty("AccessToken",
                    "刚才获取到的token");
            //允许写出
            con.setDoOutput(false);
            //允许读入
            con.setDoInput(true);
            //不使用缓存
            con.setUseCaches(false);
            //得到响应码
            int responseCode = con.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                //得到响应流 输入流
                InputStream inputStream = con.getInputStream();
                //将响应流转换成字符串
                resultBuffer = new StringBuffer();
                String line;
                // InputStreamReader类是从字节流到字符流
                buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                while ((line = buffer.readLine()) != null) {
                    resultBuffer.append(line);
                }
//                Log.e("TAG", "sendRequest: " + resultBuffer.toString());
                return getNleCloudUtils.parseJSONWithJSONObject(resultBuffer.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("TAG", "sendRequest: error " + e);
        }
        return "";
    }
}

这边给出一个非Android版本的Java Java自带没有解析JSON数据的库,这边就不解析了

class test {
    private static final String token =
            "上边获取到的token";

    public static void main(String[] args) {
        // http://www.nlecloud.com/device/534075
        String url = "https://api.nlecloud.com/Devices/Datas?devIds=535123";
        System.out.println(sendRequest(url, "GET"));

    }


    public static String sendRequest(String urlParam, String requestType) {

        HttpURLConnection con = null;
        BufferedReader buffer = null;
        StringBuffer resultBuffer = null;

        try {
            URL url = new URL(urlParam);
            //得到连接对象
            con = (HttpURLConnection) url.openConnection();
            //设置请求类型
            con.setRequestMethod(requestType);
            con.setRequestProperty("AccessToken", token);
            //设置请求需要返回的数据类型和字符集类型
            con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            //允许写出
            con.setDoOutput(true);
            //允许读入
            con.setDoInput(true);
            //不使用缓存
            con.setUseCaches(false);
            //得到响应码
            int responseCode = con.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                //得到响应流
                InputStream inputStream = con.getInputStream();
                //将响应流转换成字符串
                resultBuffer = new StringBuffer();
                String line;
                buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                while ((line = buffer.readLine()) != null) {
                    resultBuffer.append(line);
                }
                return resultBuffer.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

也只是用到了所以简单地写了一下,各位大佬见笑了。

最后还有一个问题
在这里插入图片描述
这个别过期了

  • 0
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值