Android应用从树莓派读取数据

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="当前光照强度:未知"
            android:id="@+id/tv_illuminance"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/btn_illuminance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="更新光照强度"
            tools:layout_editor_absoluteX="141dp"
            tools:layout_editor_absoluteY="184dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="当前温湿度:未知"
            android:id="@+id/tv_temhum"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/btn_temhum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="更新空气温湿度"
            tools:layout_editor_absoluteX="103dp"
            tools:layout_editor_absoluteY="143dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="当前土壤湿度:未知"
            android:id="@+id/tv_soil"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/btn_soil"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="更新土壤湿度"
            tools:layout_editor_absoluteX="201dp"
            tools:layout_editor_absoluteY="277dp" />

        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="?android:attr/listDivider"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="273dp" />

        <Button
            android:id="@+id/btn_hoststate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="更新主机运行信息"
            tools:layout_editor_absoluteX="120dp"
            tools:layout_editor_absoluteY="313dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="主机运行状态:未知"
            android:id="@+id/tv_hoststate"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chenth.httpclient">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

package com.example.chenth.httpclient;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView tv_illuminance;
    private TextView tv_temhum;
    private TextView tv_soil;
    private TextView tv_hoststate;
    private Button btn_illuminance;
    private Button btn_temhum;
    private Button btn_soil;
    private Button btn_hoststate;

    private String current_request = null;
    private String current_url = null;
    private String string_buff = null;
    private Handler handler = null;

    private Thread my_thread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (current_url==null)
                {
                    continue;
                }
                string_buff = executeHttpGet(current_url);
                //将受到的数据显示在TextView上
                if (string_buff != null) {
                    handler.post(runnableUi);
                }
            }
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        handler = new Handler();

        btn_illuminance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                current_request = "illuminance";
                current_url = "http://192.168.12.1/illuminance";
            }
        });

        btn_temhum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                current_request = "temhum";
                current_url = "http://192.168.12.1/temhum";
            }
        });

        btn_soil.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                current_request = "soil";
                current_url = "http://192.168.12.1/soil";
            }
        });

        btn_hoststate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                current_request = "hoststate";
                current_url = "http://192.168.12.1/raspberrypistate";
            }
        });

        my_thread.start();

    }

    //不能在子线程中刷新UI,应为textView是主线程建立的
    Runnable runnableUi = new Runnable() {
        @Override
        public void run() {
            if (current_request.equalsIgnoreCase("illuminance")) {
                tv_illuminance.setText(string_buff);
            }
            else if (current_request.equalsIgnoreCase("temhum")) {
                tv_temhum.setText(string_buff);
            }
            else if (current_request.equalsIgnoreCase("soil")) {
                tv_soil.setText(string_buff);
            }
            else if (current_request.equalsIgnoreCase("hoststate")) {
                tv_hoststate.setText(string_buff);
            }
            current_request = null;
            current_url = null;
        }
    };

    public String executeHttpGet(String iUrl) {
        String result = null;
        URL url = null;
        HttpURLConnection connection = null;
        InputStreamReader in = null;
        try {
            url = new URL(iUrl);
            connection = (HttpURLConnection) url.openConnection();
            in = new InputStreamReader(connection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(in);
            StringBuffer strBuffer = new StringBuffer();
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                strBuffer.append(line);
            }
            result = strBuffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return result.replace("<br>", "\r\n");
    }

    private void initView() {
        tv_hoststate = (TextView) findViewById(R.id.tv_hoststate);
        tv_illuminance = (TextView) findViewById(R.id.tv_illuminance);
        tv_temhum = (TextView) findViewById(R.id.tv_temhum);
        tv_soil = (TextView) findViewById(R.id.tv_soil);
        btn_hoststate = (Button)findViewById(R.id.btn_hoststate);
        btn_illuminance = (Button)findViewById(R.id.btn_illuminance);
        btn_temhum = (Button)findViewById(R.id.btn_temhum);
        btn_soil = (Button)findViewById(R.id.btn_soil);

    }
}
  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皓月如我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值