当前位置:   article > 正文

温湿度项目--Android APP_android实现温湿度采集

android实现温湿度采集
添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
导入 OkHttp3 依赖库
在 Module 下的 build.gradle 配置文件中的 dependencies 节点 , 进行如下配置 ;
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
非https报错的的处理办法
CLEARTEXT communication to 121.4.99.98 not permitted by network security policy
在AndroidManifest.xml application  添加 android:usesCleartextTraffic="true"
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:orientation="vertical"
  5. tools:context=".MainActivity"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent">
  8. <Button
  9. android:text="获取数据"
  10. android:layout_width="match_parent"
  11. android:layout_height="60dp"
  12. android:id="@+id/getData" />
  13. <TextView
  14. android:id="@+id/content"
  15. android:layout_width="match_parent"
  16. android:layout_height="500dp"
  17. android:background="#456789"/>
  18. </LinearLayout>
  1. package com.example.wsd;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. import org.json.JSONArray;
  9. import org.json.JSONObject;
  10. import java.io.IOException;
  11. import okhttp3.Call;
  12. import okhttp3.Callback;
  13. import okhttp3.OkHttpClient;
  14. import okhttp3.Request;
  15. import okhttp3.Response;
  16. public class MainActivity extends AppCompatActivity {
  17. private Button btn;
  18. private TextView content;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. btn = findViewById(R.id.getData);
  24. content = findViewById(R.id.content);
  25. btn.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View view) {
  28. //第一步获取okHttpClient对象
  29. OkHttpClient client = new OkHttpClient.Builder()
  30. .build();
  31. //第二步构建Request对象
  32. Request request = new Request.Builder()
  33. .url("http://121.4.99.98:5000?num=30")
  34. .get()
  35. .build();
  36. //第三步构建Call对象
  37. Call call = client.newCall(request);
  38. //第四步:异步get请求
  39. call.enqueue(new Callback() {
  40. @Override
  41. public void onFailure(Call call, IOException e) {
  42. Log.i("onFailure", e.getMessage());
  43. }
  44. @Override
  45. public void onResponse(Call call, Response response) throws IOException {
  46. String result = response.body().string(); // [[7654,"21.80","94.00","2022-11-19 19:21:54"],[7653,"21.70","94.00","2022-11-19 19:21:44"]]
  47. content.setText(result);
  48. }
  49. });
  50. }
  51. });
  52. }
  53. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/489879
推荐阅读
相关标签
  

闽ICP备14008679号