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);
}
}