1, Implementation of UI interface
This is not difficult, because the teacher gave us the renderings in advance, but one of the more complex points is to realize the image sliding, that is to use a Viewpager in the fragment to realize the image sliding.
2, Set ip address:
The code is as follows
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("Welcome, please set up the server first IP");
builder.setView(editText);
builder.setPositiveButton("Determine", 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();
}
You can then call:
private static String getaddress(String url) {
return HTTP+ StartActivity.string+HTTP2+url;
}
3, Click the button to control the light, water pump, fan and buzzer in the sand table. Android async HTTP is needed
First, add a dependency Library:
compile 'com.loopj.android:android-async-http:1.4.9'
After that, you can create a new class (here it is called HttpUtil) to encapsulate a method. The code is as follows:
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;
}
}
Then call this class in the click event you set:
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);
}
});
The control interface of JSON is as follows:
One disadvantage of using Android async HTTP is that the server receives commands too slowly. We can also use okhttp to replace it here:
It is also to create a new class to encapsulate its methods:
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){
// Create a request object
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;
}
}