How to make client android application with Flask server

Younes Belouche
May 10, 2020 · 5 min read

You will learn how to connect android studio with flask server.

Credit goes to me

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…

Credit to Hal Gatewood from unsplash

Building the Layout of the Android App

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…

By Sylvain Saurel

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:

implementation 'com.squareup.okhttp3:okhttp:4.5.0'

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:

android:usesCleartextTraffic="true"

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

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:

  1. onFailure(): Called when the request couldn’t be executed due to cancellation, a connectivity problem, or timeout.
  2. 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

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.

The handle_request() method is responsible for return a message that confirms the connection between the client and the server.

the 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.

The 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

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”.

Summary

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…