总结一下这一周关于智能农业APP项目的学习。
一、布局
APP最重要的就是与用户的交互界面,布局很重要。在智能农业APP这个项目中,布局十分简单,用户不需要花费多少时间便能熟练使用。
1、APP界面:
2、重点、难点部分布局
在布局中,没有什么难度,只有在智能农业界面中把方形的图片设置成圆形时需要注意一下。
首先在gradle中添加依赖:
compile 'com.makeramen:roundedimageview:2.2.1'
然后布局中代码如下所示:
<com.makeramen.roundedimageview.RoundedImageView
app:riv_oval="true"
android:id="@+id/light_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@mipmap/guangzhao" />
如果没有app:riv_oval="true"
这句话,那么图片还是不会变成圆形的。
二、Java代码
1、添加依赖
由于需要gson解析、使用OKHttp快速访问网络,所以需要添加两个依赖:
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.okhttp3:okhttp:3.10.0'
2、从首页进入后的三个界面——智能农业界面、设置界面、帮助界面
- 创建三个fragment——HomePagerFragment、SettingFragment、HelpFragment。
在HomePagerFragment中把首页布局文件写好:
<TextView
android:id="@+id/home_page_top_lin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:text="智能农业"
android:textColor="#24bd3b"
android:textSize="30dp" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/home_page_top_lin"
android:background="#b5b5b5"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/home_page_top_lin"
android:overScrollMode="never"
android:scrollbars="none"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/home_page_vp"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@mipmap/bana1">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<com.makeramen.roundedimageview.RoundedImageView
app:riv_oval="true"
android:id="@+id/co2_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@mipmap/co2" />
<TextView
android:layout_toLeftOf="@+id/co2_warn_img"
android:id="@+id/co2_concentation_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/co2_img"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/co2_img"
android:text="CO2浓度"
android:textColor="#100909"
android:textSize="25sp" />
<RelativeLayout
android:layout_toLeftOf="@+id/co2_right_img"
android:id="@+id/co2_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/co2_concentation_tv"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/co2_img">
<TextView
android:id="@+id/co2_co2_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="CO2:"
android:textColor="#100909"
android:textSize="25sp" />
<TextView
android:id="@+id/co2_values_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/co2_co2_tv"
android:gravity="center_vertical"
android:text="200"
android:textColor="#e80c0c"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_toLeftOf="@+id/co2_right_img"
android:layout_alignBottom="@+id/co2_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/co2_relative"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/co2_img">
<TextView
android:id="@+id/co2_set_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="设定值:"
android:textColor="#1fa433"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/co2_set_tv"
android:gravity="center_vertical"
android:text="200"
android:textColor="#605858"
android:textSize="15sp" />
</RelativeLayout>
<ImageView
android:id="@+id/co2_right_img"
android:layout_width="20dp"
android:src="@mipmap/right"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_height="60dp" />
<ImageView
android:id="@+id/co2_warn_img"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@mipmap/p2"
android:layout_width="50dp"
android:layout_height="20dp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<com.makeramen.roundedimageview.RoundedImageView
app:riv_oval="true"
android:id="@+id/light_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@mipmap/guangzhao" />
<TextView
android:id="@+id/light_intension_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/light_img"
android:layout_margin="5dp"
android:layout_toLeftOf="@+id/light_warn_img"
android:layout_toRightOf="@+id/light_img"
android:text="光照强度"
android:textColor="#100909"
android:textSize="25sp" />
<RelativeLayout
android:id="@+id/light_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/light_intension_tv"
android:layout_margin="5dp"
android:layout_toLeftOf="@+id/light_right_img"
android:layout_toRightOf="@+id/light_img">
<TextView
android:id="@+id/light_light_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="光照"
android:textColor="#100909"
android:textSize="25sp" />
<TextView
android:id="@+id/light_values_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/light_light_tv"
android:gravity="center_vertical"
android:text="200"
android:textColor="#e80c0c"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/light_img"
android:layout_below="@+id/light_relative"
android:layout_margin="5dp"
android:layout_toLeftOf="@+id/light_right_img"
android:layout_toRightOf="@+id/light_img">
<TextView
android:id="@+id/light_set_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="设定值:"
android:textColor="#1fa433"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/light_set_tv"
android:gravity="center_vertical"
android:text="200"
android:textColor="#605858"
android:textSize="15sp" />
</RelativeLayout>
<ImageView
android:id="@+id/light_right_img"
android:layout_width="20dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/right" />
<ImageView
android:id="@+id/light_warn_img"
android:layout_width="50dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@mipmap/p2" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<com.makeramen.roundedimageview.RoundedImageView
app:riv_oval="true"
android:id="@+id/soil_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@mipmap/turang" />
<TextView
android:id="@+id/soil_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/soil_img"
android:layout_margin="5dp"
android:layout_toLeftOf="@+id/soil_warn_img"
android:layout_toRightOf="@+id/soil_img"
android:text="土壤指数"
android:textColor="#100909"
android:textSize="20sp" />
<RelativeLayout
android:id="@+id/light_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/soil_tv"
android:layout_margin="5dp"
android:layout_toLeftOf="@+id/soil_right_img"
android:layout_toRightOf="@+id/soil_img">
<TextView
android:id="@+id/soil_temp_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="温度:"
android:textColor="#100909"
android:textSize="20sp" />
<TextView
android:id="@+id/soil_temp_values_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/soil_temp_tv"
android:gravity="center_vertical"
android:text="35"
android:textColor="#e80c0c"
android:textSize="20sp" />
<TextView
android:layout_toRightOf="@+id/soil_temp_values_tv"
android:id="@+id/soil_wetness_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="湿度:"
android:textColor="#100909"
android:textSize="20sp" />
<TextView
android:id="@+id/soil_wetness_values_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/soil_wetness_tv"
android:gravity="center_vertical"
android:text="35"
android:textColor="#e80c0c"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/soil_set_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_below="@+id/light_relative"
android:layout_toLeftOf="@+id/soil_right_img"
android:layout_toRightOf="@+id/soil_img">
<TextView
android:id="@+id/soil_temp_set_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="温度设定值:"
android:textColor="#1fa433"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/soil_temp_set_tv"
android:gravity="center_vertical"
android:text="200"
android:textColor="#605858"
android:textSize="15sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_alignBottom="@+id/soil_img"
android:layout_below="@+id/soil_set_relative"
android:layout_toLeftOf="@+id/soil_right_img"
android:layout_toRightOf="@+id/soil_img">
<TextView
android:id="@+id/soil_wetness_set_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="湿度设定值:"
android:textColor="#1fa433"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/soil_wetness_set_tv"
android:gravity="center_vertical"
android:text="200"
android:textColor="#605858"
android:textSize="15sp" />
</RelativeLayout>
<ImageView
android:id="@+id/soil_right_img"
android:layout_width="20dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/right" />
<ImageView
android:id="@+id/soil_warn_img"
android:layout_width="50dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@mipmap/p2" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<com.makeramen.roundedimageview.RoundedImageView
app:riv_oval="true"
android:id="@+id/soil_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@mipmap/kongqi" />
<TextView
android:id="@+id/soil_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/soil_img"
android:layout_margin="5dp"
android:layout_toLeftOf="@+id/air_warn_img"
android:layout_toRightOf="@+id/soil_img"
android:text="空气指数"
android:textColor="#100909"
android:textSize="20sp" />
<RelativeLayout
android:id="@+id/light_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/soil_tv"
android:layout_margin="5dp"
android:layout_toLeftOf="@+id/air_right_img"
android:layout_toRightOf="@+id/soil_img">
<TextView
android:id="@+id/soil_temp_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="温度:"
android:textColor="#100909"
android:textSize="20sp" />
<TextView
android:id="@+id/air_temp_values_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/soil_temp_tv"
android:gravity="center_vertical"
android:text="35"
android:textColor="#e80c0c"
android:textSize="20sp" />
<TextView
android:layout_toRightOf="@+id/air_temp_values_tv"
android:id="@+id/air_wetness_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="湿度:"
android:textColor="#100909"
android:textSize="20sp" />
<TextView
android:id="@+id/air_wetness_values_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/air_wetness_tv"
android:gravity="center_vertical"
android:text="35"
android:textColor="#e80c0c"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/soil_set_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_below="@+id/light_relative"
android:layout_toLeftOf="@+id/air_right_img"
android:layout_toRightOf="@+id/soil_img">
<TextView
android:id="@+id/soil_temp_set_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="温度设定值:"
android:textColor="#1fa433"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/soil_temp_set_tv"
android:gravity="center_vertical"
android:text="200"
android:textColor="#605858"
android:textSize="15sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_alignBottom="@+id/soil_img"
android:layout_below="@+id/soil_set_relative"
android:layout_toLeftOf="@+id/air_right_img"
android:layout_toRightOf="@+id/soil_img">
<TextView
android:id="@+id/soil_wetness_set_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="湿度设定值:"
android:textColor="#1fa433"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/soil_wetness_set_tv"
android:gravity="center_vertical"
android:text="200"
android:textColor="#605858"
android:textSize="15sp" />
</RelativeLayout>
<ImageView
android:id="@+id/air_right_img"
android:layout_width="20dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/right" />
<ImageView
android:id="@+id/air_warn_img"
android:layout_width="50dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@mipmap/p2" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
HelpFragment、SettingFragment中的布局就不在展示。
- 创建适配器MyPAgerAdapter
public class MyPageAdapter extends FragmentPagerAdapter {
//创建fragmentList集合
private List<Fragment> fragmentList;
//构造方法实现传值
public MyPageAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
//返回fragmentList对象
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
//返回fragmentList对象个数
@Override
public int getCount() {
return fragmentList.size();
}
}
- 在MainActivity中绑定适配器,使得三个界面可以自由滑动。
HomePageFragment homePageFragment = new HomePageFragment();
SetFragment setFragment = new SetFragment();
HelpFragment helpFragment = new HelpFragment();
fragmentList.add(homePageFragment);
fragmentList.add(setFragment);
fragmentList.add(helpFragment);
MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager(), fragmentList);
agricultureVp.setAdapter(adapter);
3、首页对话框
在每次打开APP时,都会弹出一个对话框,提示输入服务器的IP地址。
- 在布局文件中先写好对话框的布局代码:
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20dp"
android:text="欢迎您,请先设置服务器IP"
android:gravity="center"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
<EditText
android:id="@+id/dialog_et"
android:layout_width="match_parent"
android:hint="请输入设备上显示的IP地址"
android:padding="15dp"
android:layout_height="50dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#716c6c"/>
<Button
android:id="@+id/dialog_btn"
android:layout_width="match_parent"
android:layout_margin="15dp"
android:text="确定"
android:textSize="20sp"
android:gravity="center"
android:layout_gravity="center"
android:layout_height="50dp"
android:background="#63c999"
/>
- 在背景图片对应的Activity中创建对话框
handler.postDelayed(new Runnable() {
//使用handler的postDelayed实现延时跳转
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(ImgActivity.this);
View view = View.inflate(ImgActivity.this,R.layout.alert_dialog,null);
builder.setView(view);
builder.setCancelable(true);
editText = view.findViewById(R.id.dialog_et);
button = view.findViewById(R.id.dialog_btn);
AlertDialog dialog = builder.create();
dialog.show();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ImgActivity.this, MainActivity.class);
IP = editText.getText().toString();
intent.putExtra("IP",IP);
startActivity(intent);
finish();
}
});
}
}, SPLASH_LENGTH);
//3秒后跳转至应用主界面MainActivity
- 接收IP地址
在智能农业首页HomePagerFragment中接收IP地址:
Intent intent = getActivity().getIntent();
String relativeUrl = intent.getStringExtra("IP");
5、从HomePagerFragment中查看CO2、空气、土壤、光照详情
创建四个Activity——Co2Activity、AirActivity、SoilActivity、SunshineActivity。
从Fragment到Activity之间的通信与传值用如下语句:
扫描二维码关注公众号,回复: 26286 查看本文章
Intent intent = new Intent(getActivity(), CO2Activity.class);
intent.putExtra("basicUrl",basicUrl);
startActivity(intent);
6、CO2、空气、土壤、光照详情
布局文件就不再叙述。
- 创建Gson解析的实体类:
public class App {
private int airHumidity;
private int PM25;
private int airTemperature;
private int soilTemperature;
private int co2;
private int soilHumidity;
private int light;
public int getAirHumidity() {
return airHumidity;
}
public void setAirHumidity(int airHumidity) {
this.airHumidity = airHumidity;
}
public int getPM25() {
return PM25;
}
public void setPM25(int PM25) {
this.PM25 = PM25;
}
public int getAirTemperature() {
return airTemperature;
}
public void setAirTemperature(int airTemperature) {
this.airTemperature = airTemperature;
}
public int getSoilTemperature() {
return soilTemperature;
}
public void setSoilTemperature(int soilTemperature) {
this.soilTemperature = soilTemperature;
}
public int getCo2() {
return co2;
}
public void setCo2(int co2) {
this.co2 = co2;
}
public int getSoilHumidity() {
return soilHumidity;
}
public void setSoilHumidity(int soilHumidity) {
this.soilHumidity = soilHumidity;
}
public int getLight() {
return light;
}
public void setLight(int light) {
this.light = light;
}
}
public class AppStatus {
private int WaterPump;
private int Blower;
private int Roadlamp;
private int Buzzer;
private String result;
public int getWaterPump() {
return WaterPump;
}
public void setWaterPump(int waterPump) {
WaterPump = waterPump;
}
public int getBlower() {
return Blower;
}
public void setBlower(int blower) {
Blower = blower;
}
public int getRoadlamp() {
return Roadlamp;
}
public void setRoadlamp(int roadlamp) {
Roadlamp = roadlamp;
}
public int getBuzzer() {
return Buzzer;
}
public void setBuzzer(int buzzer) {
Buzzer = buzzer;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
public class FanResult {
private String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
- 创建OKHttp用来请求网络的实体类:
public class Okhttp {
private static OkHttpClient client = new OkHttpClient();
public static void sendOkHttpRequest(String address,okhttp3.Callback callback){
Request request = new Request.Builder().url(address).build();
client.newCall(request).enqueue(callback);
}
public static void postJsonByOkHttp(String url, JSONObject jsonObject,okhttp3.Callback callback){
MediaType mediaType = MediaType.parse("application/json;Charset=UTF-8");
RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString());
Request request = new Request.Builder().url(url).post(requestBody).build();
client.newCall(request).enqueue(callback);
}
- 把控制灯光、水泵、蜂鸣器、风扇封装在同一个类中。
public class Control {
public static int Result = 0;
public static int alertResult = 0;
public static int lightResult = 0;
public static int pumpResult = 0;
//开风扇
public static void openFan(String basicUrl, final Context context) {
final JSONObject[] jsonObject = {new JSONObject()};
try {
jsonObject[0].put("Blower", 1);
} catch (JSONException e) {
e.printStackTrace();
}
Okhttp.postJsonByOkHttp(basicUrl +"control", jsonObject[0], new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseString = response.body().string();
try {
JSONObject jsonObject1 = new JSONObject(responseString);
if (jsonObject1.getString("result").equals("ok")) {
Status.Blower = 1;
Control.Result = 1;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
//关风扇
public static void closeFan(String basicUrl, Context context) {
final JSONObject[] jsonObject = {new JSONObject()};
try {
jsonObject[0].put("Blower", 0);
} catch (JSONException e) {
e.printStackTrace();
}
Okhttp.postJsonByOkHttp(basicUrl +"control", jsonObject[0], new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseString = response.body().string();
try {
JSONObject jsonObject1 = new JSONObject(responseString);
if (jsonObject1.getString("result").equals("ok")) {
Status.Blower = 0;
Control.Result = 0;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
//开蜂鸣器
public static void openAlert(String basicUrl, Context context) {
final JSONObject[] jsonObject = {new JSONObject()};
try {
jsonObject[0].put("Buzzer", 1);
} catch (JSONException e) {
e.printStackTrace();
}
Okhttp.postJsonByOkHttp(basicUrl +"control", jsonObject[0], new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseString = response.body().string();
try {
JSONObject jsonObject1 = new JSONObject(responseString);
if (jsonObject1.getString("result").equals("ok")) {
Control.alertResult = 1;
Status.Buzzer = 1;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
//关蜂鸣器
public static void closeAlert(String basicUrl, Context context) {
final JSONObject[] jsonObject = {new JSONObject()};
try {
jsonObject[0].put("Buzzer", 0);
} catch (JSONException e) {
e.printStackTrace();
}
Okhttp.postJsonByOkHttp(basicUrl +"control", jsonObject[0], new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseString = response.body().string();
try {
JSONObject jsonObject1 = new JSONObject(responseString);
if (jsonObject1.getString("result").equals("ok")) {
Control.alertResult = 0;
Status.Buzzer = 0;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
//开灯
public static void openLight(String basicUrl, Context context) {
final JSONObject[] jsonObject = {new JSONObject()};
try {
jsonObject[0].put("Roadlamp", 1);
} catch (JSONException e) {
e.printStackTrace();
}
Okhttp.postJsonByOkHttp(basicUrl +"control", jsonObject[0], new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseString = response.body().string();
try {
JSONObject jsonObject1 = new JSONObject(responseString);
if (jsonObject1.getString("result").equals("ok")) {
Control.lightResult = 1;
Status.Roadlamp = 1;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
//关灯
public static void closeLight(String basicUrl, Context context) {
final JSONObject[] jsonObject = {new JSONObject()};
try {
jsonObject[0].put("Roadlamp", 0);
} catch (JSONException e) {
e.printStackTrace();
}
Okhttp.postJsonByOkHttp(basicUrl +"control", jsonObject[0], new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseString = response.body().string();
try {
JSONObject jsonObject1 = new JSONObject(responseString);
if (jsonObject1.getString("result").equals("ok")) {
Control.lightResult = 0;
Status.Roadlamp = 0;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
//开水泵
public static void openPump(String basicUrl, Context context) {
final JSONObject[] jsonObject = {new JSONObject()};
try {
jsonObject[0].put("WaterPump", 1);
} catch (JSONException e) {
e.printStackTrace();
}
Okhttp.postJsonByOkHttp(basicUrl +"control", jsonObject[0], new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseString = response.body().string();
try {
JSONObject jsonObject1 = new JSONObject(responseString);
if (jsonObject1.getString("result").equals("ok")) {
Control.pumpResult = 1;
Status.WaterPump = 1;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
//关水泵
public static void closePump(String basicUrl, Context context) {
final JSONObject[] jsonObject = {new JSONObject()};
try {
jsonObject[0].put("WaterPump", 0);
} catch (JSONException e) {
e.printStackTrace();
}
Okhttp.postJsonByOkHttp(basicUrl +"control", jsonObject[0], new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseString = response.body().string();
try {
JSONObject jsonObject1 = new JSONObject(responseString);
if (jsonObject1.getString("result").equals("ok")) {
Control.pumpResult = 0;
Status.WaterPump = 0;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
- 创建一个标志位用来判断灯光、蜂鸣器、水泵、风扇是否呈关闭状态。
public class Status {
public static int WaterPump = 0;
public static int Blower = 0;
public static int Roadlamp = 0;
public static int Buzzer = 0;
public static String resultStr;
public static String TAG = "Status";
}
- 最后,最重要的是在四个Activity中调用上面封装的类的开关。
Co2Acvitity:
public class CO2Activity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "CO2Activity";
private static ImageView openFanImg;
private static ImageView openAlertImg;
private TextView co2ValuseTv;
private void modifierBlowerStatus() {
if (Status.Blower == 0) {
openFanImg.setImageResource(R.mipmap.dakaifengshan);
Control.Result = 0;
} else if (Status.Blower == 1) {
openFanImg.setImageResource(R.mipmap.dakaifengshan2);
Control.Result = 1;
}
}
private void modifierBuzzerStatus() {
if (Status.Buzzer == 0) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing);
Control.alertResult = 0;
} else if (Status.Buzzer == 1) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing2);
Control.alertResult = 1;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_co2);
bangID();
modifierBlowerStatus();
modifierBuzzerStatus();
App app = new App();
co2ValuseTv.setText(HomePageFragment.co2+"");
}
private void bangID() {
openFanImg = findViewById(R.id.co2_open_fan_img);
openAlertImg = findViewById(R.id.co2_openalert_img);
co2ValuseTv = findViewById(R.id.co_values_tv);
openFanImg.setOnClickListener(this);
openAlertImg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.co2_open_fan_img:
if (Control.Result == 0) {
openFanImg.setImageResource(R.mipmap.dakaifengshan2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openFan(basicUrl, CO2Activity.this);
} else if (Control.Result == 1) {
openFanImg.setImageResource(R.mipmap.dakaifengshan);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closeFan(basicUrl, CO2Activity.this);
}
break;
case R.id.co2_openalert_img:
if (Control.alertResult == 0) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openAlert(basicUrl, CO2Activity.this);
} else if (Control.alertResult == 1) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closeAlert(basicUrl, CO2Activity.this);
}
break;
}
}
}
SoilActivity:
public class SoilActivity extends AppCompatActivity implements View.OnClickListener{
private static ImageView openPumpImg;
private static ImageView openLightImg;
private static ImageView openAlertImg;
private TextView soilTemp;
private TextView soilWet;
private void modifierPumpStatus() {
if (Status.WaterPump == 0) {
openPumpImg.setImageResource(R.mipmap.dakaishui);
Control.pumpResult = 0;
} else if (Status.WaterPump == 1) {
openPumpImg.setImageResource(R.mipmap.dakaishui2);
Control.pumpResult = 1;
}
}
private void modifierLightStatus() {
if (Status.Roadlamp == 0) {
openLightImg.setImageResource(R.mipmap.dakaiguangzhao);
Control.lightResult = 0;
} else if (Status.Roadlamp == 1) {
openLightImg.setImageResource(R.mipmap.dakaiguangzhao2);
Control.lightResult = 1;
}
}
private void modifierBuzzerStatus() {
if (Status.Buzzer == 0) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing);
Control.alertResult = 0;
} else if (Status.Buzzer == 1) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing2);
Control.alertResult = 1;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soil);
bangID();
modifierPumpStatus();
modifierLightStatus();
modifierBuzzerStatus();
soilWet.setText(HomePageFragment.soilHumidity+"");
soilTemp.setText(HomePageFragment.soilTemperature+"");
}
private void bangID() {
openPumpImg = findViewById(R.id.soil_WaterPump_img);
openLightImg = findViewById(R.id.soil_light_img);
openAlertImg = findViewById(R.id.soil_alert_img);
soilTemp = findViewById(R.id.soil_temp_values);
soilWet = findViewById(R.id.soil_wet_values);
openPumpImg.setOnClickListener(this);
openLightImg.setOnClickListener(this);
openAlertImg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.soil_WaterPump_img:
if (Control.pumpResult == 0) {
openPumpImg.setImageResource(R.mipmap.dakaishui2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openPump(basicUrl, SoilActivity.this);
// Toast.makeText(this, "开水泵", Toast.LENGTH_SHORT).show();
} else if (Control.pumpResult == 1) {
openPumpImg.setImageResource(R.mipmap.dakaishui);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closePump(basicUrl, SoilActivity.this);
// Toast.makeText(this, "关水泵", Toast.LENGTH_SHORT).show();
}
break;
case R.id.soil_light_img:
if(Control.lightResult==0){
openLightImg.setImageResource(R.mipmap.dakaiguangzhao2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openLight(basicUrl,SoilActivity.this);
// Toast.makeText(this, "开灯", Toast.LENGTH_SHORT).show();
}else if( Control.lightResult==1){
openLightImg.setImageResource(R.mipmap.dakaiguangzhao);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closeLight(basicUrl,SoilActivity.this);
// Toast.makeText(this, "关灯", Toast.LENGTH_SHORT).show();
}
break;
case R.id.soil_alert_img:
if (Control.alertResult == 0) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openAlert(basicUrl, SoilActivity.this);
// Toast.makeText(this, "开警报", Toast.LENGTH_SHORT).show();
} else if (Control.alertResult == 1) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closeAlert(basicUrl, SoilActivity.this);
// Toast.makeText(this, "关警报", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
AirActivity:
public class AirActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView openFanImg;
private ImageView openLightImg;
private ImageView openPumpImg;
private ImageView openAlertImg;
private TextView airTemp;
private TextView airWet;
private void modifierBlowerStatus() {
if (Status.Blower == 0) {
openFanImg.setImageResource(R.mipmap.dakaifengshan);
Control.Result = 0;
} else if (Status.Blower == 1) {
openFanImg.setImageResource(R.mipmap.dakaifengshan2);
Control.Result = 1;
}
}
private void modifierBuzzerStatus() {
if (Status.Buzzer == 0) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing);
Control.alertResult = 0;
} else if (Status.Buzzer == 1) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing2);
Control.alertResult = 1;
}
}
private void modifierLightStatus() {
if (Status.Roadlamp == 0) {
openLightImg.setImageResource(R.mipmap.dakaiguangzhao);
Control.lightResult = 0;
} else if (Status.Roadlamp == 1) {
openLightImg.setImageResource(R.mipmap.dakaiguangzhao2);
Control.lightResult = 1;
}
}
private void modifierPumpStatus() {
if (Status.WaterPump == 0) {
openPumpImg.setImageResource(R.mipmap.dakaishui);
Control.pumpResult = 0;
} else if (Status.WaterPump == 1) {
openPumpImg.setImageResource(R.mipmap.dakaishui2);
Control.pumpResult = 1;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_air);
bangID();
modifierBlowerStatus();
modifierBuzzerStatus();
modifierLightStatus();
modifierPumpStatus();
airTemp.setText(HomePageFragment.airTemperature+"");
airWet.setText(HomePageFragment.airHumidity+"");
}
private void bangID() {
openFanImg = findViewById(R.id.air_fan_img);
openLightImg = findViewById(R.id.air_light_img);
openPumpImg = findViewById(R.id.air_pump_img);
openAlertImg = findViewById(R.id.air_alert_img);
airTemp = findViewById(R.id.air_temp_values);
airWet = findViewById(R.id.air_wet_values);
openFanImg.setOnClickListener(this);
openLightImg.setOnClickListener(this);
openPumpImg.setOnClickListener(this);
openAlertImg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.air_alert_img:
if (Control.alertResult == 0) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openAlert(basicUrl, AirActivity.this);
} else if (Control.alertResult == 1) {
openAlertImg.setImageResource(R.mipmap.dakaibaojing);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closeAlert(basicUrl, AirActivity.this);
}
break;
case R.id.air_fan_img:
if (Control.Result == 0) {
openFanImg.setImageResource(R.mipmap.dakaifengshan2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openFan(basicUrl, AirActivity.this);
} else if (Control.Result == 1) {
openFanImg.setImageResource(R.mipmap.dakaifengshan);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closeFan(basicUrl, AirActivity.this);
}
break;
case R.id.air_light_img:
if(Control.lightResult==0){
openLightImg.setImageResource(R.mipmap.dakaiguangzhao2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openLight(basicUrl,AirActivity.this);
}else if( Control.lightResult==1){
openLightImg.setImageResource(R.mipmap.dakaiguangzhao);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closeLight(basicUrl,AirActivity.this);
}
break;
case R.id.air_pump_img:
if (Control.pumpResult == 0) {
openPumpImg.setImageResource(R.mipmap.dakaishui2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openPump(basicUrl, AirActivity.this);
} else if (Control.pumpResult == 1) {
openPumpImg.setImageResource(R.mipmap.dakaishui);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closePump(basicUrl, AirActivity.this);
}
break;
}
}
}
LightActivity:
public class LightActivity extends AppCompatActivity implements View.OnClickListener{
private static ImageView opneLightImg;
private static ImageView opneAlertImg;
private TextView lightTv;
private void modifierLightStatus() {
if (Status.Roadlamp == 0) {
opneLightImg.setImageResource(R.mipmap.dakaiguangzhao);
Control.lightResult = 0;
} else if (Status.Roadlamp == 1) {
opneLightImg.setImageResource(R.mipmap.dakaiguangzhao2);
Control.lightResult = 1;
}
}
private void modifierBuzzerStatus() {
if (Status.Buzzer == 0) {
opneAlertImg.setImageResource(R.mipmap.dakaibaojing);
Control.alertResult = 0;
} else if (Status.Buzzer == 1) {
opneAlertImg.setImageResource(R.mipmap.dakaibaojing2);
Control.alertResult = 1;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light);
bangID();
modifierLightStatus();
modifierBuzzerStatus();
App app = new App();
lightTv.setText(HomePageFragment.light+"");
}
private void bangID() {
opneLightImg = findViewById(R.id.light_openLight_img);
opneAlertImg = findViewById(R.id.light_alert_img);
lightTv = findViewById(R.id.light_values_tv2);
opneLightImg.setOnClickListener(this);
opneAlertImg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.light_openLight_img:
if(Control.lightResult==0){
opneLightImg.setImageResource(R.mipmap.dakaiguangzhao2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openLight(basicUrl,LightActivity.this);
}else if( Control.lightResult==1){
opneLightImg.setImageResource(R.mipmap.dakaiguangzhao);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closeLight(basicUrl,LightActivity.this);
}
break;
case R.id.light_alert_img:
if (Control.alertResult == 0) {
opneAlertImg.setImageResource(R.mipmap.dakaibaojing2);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.openAlert(basicUrl, LightActivity.this);
} else if (Control.alertResult == 1) {
opneAlertImg.setImageResource(R.mipmap.dakaibaojing);
Intent intent = getIntent();
String basicUrl = intent.getStringExtra("basicUrl");
Control.closeAlert(basicUrl,LightActivity.this);
}
break;
}
}
}
三、OkHttp使用
1、网络请求原理:
2、创建OKHttp实例:
三种创建实例的方法:
创建一个默认配置OkHttpClient,可以使用默认的构造函数。
通过new OkHttpClient.Builder()方法来一步一步配置一个OkHttpClient实例。
如果要求使用现有的实例,可以通过newBuilder()方法来进行构造。
request
简单看一下Request类,可以发现它代表一个Http请求,需要注意的是Request一旦build()之后,便不可修改。response
Response代表一个Http的响应,这个类的实例不可修改。- 发起请求
跟到newCall()方法中发现,又使用OkHttpClient实例和Request的实例,一起构造了一个RealCall的实例。RealCall类简单做了一个托管并通过Dispather类对请求进行分发和执行,实际开启线程发起请求的方法就在这个类中。随后又调用execute()方法,拿到了一个响应。这个execute()方法,实际上执行的就是RealCall中的execute()方法,最后调用了Dispatcher的execute()方法。 - 两个需要注意的点:
okhttp3不能在ui线程中运行
onresponse在子线程中,需要用handler回调才能操作ui
over~