You will learn how to connect android studio with flask server.
We all know that mobile devices are not powerful compared to PCs, we can do a lot of things with our smart phones but with limited resources, most of us consider that like a weakness point.
So, from the previous paragraph, we can conclude that mobile devices are not the best choice for many things such as machine learning.When we say machine learning we must also mention deep learning, because this last one is included in it, and for the record, deep neural network needs a lot of samples to be trained with so we need a lot of resources On the other hand.
Today, we will learn how to connect a simple android client side application made with java to python flask server, after reading this article you will be able to deploy you machine learning models.You will need Android studio, The HTTP requests and responses are handled in Android using OkHttp and Pycharm IDE for python coding.
This tutorial will show you how to send a simple text in which the Android app just makes an HTTP POST message for sending it, in the other hand, the flask app will return a simple message to confirm that the connection is successful.
The implementation of this project is available on GitHub: Click here.
Enough talking, let’s get started…
Building the Layout of the Android App
Our app is designed to be simple, but this will not prevent you form improving it in the future.So I decided to make one button in the main UI, after you click this button, it will test the connection between the android app and the flask app, the message returned from the flask app will displayed as Toast notification.
You can see that we used only one button with ID = connect.
After building the app user interface (UI), the next step is to implement the postRequest
() method, which is responsible for message sending.
But, for doing that we must go through a small process…
First, before using OkHttp, the project must support using it.You can do that by including one line to the dependencies section of the build.gradle (app module), the line is:
Second, you must activate the using of clear text traffic in your app, in you can do that by inserting a this line to your application section in the manifest file:
It will be like this:
But we don’t forget to add the next permission in the manifest file:
<uses-permission android:name="android.permission.INTERNET" />
without it, you application will crash.
Writing the java code of the Android App
Now, we will move to java code.
When you start reading the code, you will notice that we use the IP@ 10.0.2.2, this is just the default local address of the android emulator, and 5000 is the port number that we will use with it.You will some words like mediaType and requestBody, mediaType is just type the data that we will send such as text or image…etc, requestBody is the object that holds our message.These are some methods in the code that you must understand them:
onFailure(): Called when the request couldn’t be executed due to cancellation, a connectivity problem, or timeout.
onResponse(): Called when the HTTP response is successfully returned by the remote server.
The rest of the code will be easy to understand, But if you run into some problems, you can always go back to the OkHttp documentation from here.
Now, you can test the app but it will show an error message because the server is off.While we mentioned the server, let’s continue building the Python HTTP server using Flask.
Building the server-side using Flask
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself.From Wikipedia, the free encyclopedia.
From the picture above, you see that we created a flask app as an instance of the
Flask() class, The
app.route() decorator function associates a URL with a callback function.The
/indicates the homepage of the server because the Android app requests will be sent to there.
handle_request() method is responsible for return a message that confirms the connection between the client and the server.
run() method uses the
host argument for specifying the IP address. When the
host is set to
“0.0.0.0”, this means use the current IPv4 address. And of course,
5000as port number.
debug argument is for giving the permission to server for restarting itself if the code changed, otherwise you will be obligated to restart it manually to apply the new changes.
The last step: Testing
You will need an internet connection.
Now you will be able to test you app, you have to run the android app and also the python code, clicking the connect button and then you will see that it will return “Successful Connection”.
In this post you discovered how to build a client android application with python flask server.You can read an article to learn how to save your machine learning model to deploy it later, you just have to click here, if you want to contact me, this my LinkedIn account, I hope you enjoyed, don’t for forget to hit Clap to encourage me :)