Invoking the Arduino services
In this paragraph, we will explore how to invoke the Arduino services exposed using the Rest paradigm. In this context, we have to call the services passing the data defined in the user interface exposed by our Android Things app. While in the previous chapter we used the Volley library, in this chapter we will use another library to handle the HTTP connection called OkHTTP
(http://square.github.io/okhttp/). In this way, you use another approach and you can select the best one according to your requirements.
The first thing to do is add the library to our build.gradle
file:
compile 'com.squareup.okhttp3:okhttp:3.6.0'
Now we can create another class to handle the communication details with the Arduino board:
- Create a new class called
BoardController.java
. - Add a private constructor because this class must be a singleton:
private BoardController() { client = new OkHttpClient(); } public static BoardController getInstance() { if ...