智能农业项目的感悟

一.UI界面的实现
这个没什么难度,因为老师提前给了我们效果图,但其中比较复杂的一点就是实现图片的滑动,就是在fragment中再用一个Viewpager来实现图片的滑动。
二.设置ip地址:
这里写图片描述
代码如下

 public static String string;

 private void modify() {
        final EditText editText = new EditText(this);
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("欢迎您,请先设置服务器IP");
        builder.setView(editText);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                string=editText.getText().toString();
                Intent intent = new Intent(StartActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        builder.show();
    }

之后可调用:

 private static String getaddress(String url) {
              return HTTP+ StartActivity.string+HTTP2+url;
    }

三 .点击按钮控制沙盘中的灯光,水泵,风扇,蜂鸣器,其中需要用到android-async-http(不熟悉的朋友可看我上一篇文章):
首先先添加依赖库:

 compile 'com.loopj.android:android-async-http:1.4.9'

之后可以新建一个类(这里就叫HttpUtil吧)去封装一个方法,代码如下:

package com.example.administrator.myintent;

import com.loopj.android.http.*;

/**
 * Created by Administrator on 2018/4/10/010.
 */

public class HttpUtil {
    private static final String BAse_url = "http://192.168.1.105:8890/type/jason/action/";
    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void Post(android.content.Context context, java.lang.String url, cz.msebera.android.httpclient.HttpEntity entity, java.lang.String contentType, ResponseHandlerInterface responseHandler) {
        client.post(context,getAbsoluteUrl(url), entity, contentType,responseHandler);
    }

    private static String getAbsoluteUrl(String url) {

        return BAse_url + url;
    }

}

之后在你设置的点击事件里调用这个类就行:

JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("Buzzer", 1);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                ByteArrayEntity entity = null;
                try {
                    entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
                    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                HttpUtil.Post(mContext, "control", entity, "application/json", new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        Log.e("rs", response.toString());
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                        super.onFailure(statusCode, headers, throwable, errorResponse);
                    }
                });

JSON的控制接口如下:

这里写图片描述

用android-async-http有一缺点就是服务器接收命令速度太慢,我们这里也可用okhttp来代替:
同样是新建一个类封装它的方法:

package com.example.intellsigentagriculture.use;

import android.util.Log;

import com.example.intellsigentagriculture.StartActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URL;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by wang3 on 2018/4/12.
 */

public class OkHttp {
    private static final String HTTP="http://";
    private static final String HTTP2= ":8890/type/jason/action/";
    private static  OkHttpClient okHttpClient=new OkHttpClient();
    public static final MediaType Json=MediaType.parse("application/json;charset=UTF-8");

    public static void get(String url, Callback callback){
        // 创建一个request对象
        Request request = new Request.Builder().url(getaddress(url)).build();
        Call call= okHttpClient.newCall(request);
        call.enqueue(callback);
    }
    public static void post(String url,String json,int number, Callback callback){
        try {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put(json,number);
            RequestBody requestBody=RequestBody.create(Json,jsonObject.toString());
            Request request = new Request.Builder().url(getaddress(url)).post(requestBody).build();
            okHttpClient.newCall(request).enqueue(callback);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private static String getaddress(String url) {
        Log.e("000000000",  HTTP+ StartActivity.string+HTTP2+url);
        return HTTP+ StartActivity.string+HTTP2+url;
    }
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页