title: How to connect android app to python flask server?
body: <p>I am trying to create a simple android app that comunicates with a Flask python server
  Here is the app code:</p>
  <p>activity_main.xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout 
  xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;match_parent&quot;
  tools:context=&quot;.MainActivity&quot;&gt;
  
  &lt;TextView
      android:id=&quot;@+id/textview&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
      app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
      app:layout_constraintRight_toRightOf=&quot;parent&quot;
      app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p>MainActivity.java:</p>
  <pre><code>package com.example.flasktest2;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.os.Bundle;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import org.jetbrains.annotations.NotNull;
  
  import java.io.IOException;
  
  import okhttp3.Call;
  import okhttp3.Callback;
  import okhttp3.OkHttpClient;
  import okhttp3.Request;
  import okhttp3.Response;
  
  public class MainActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
      OkHttpClient okHttpClient = new OkHttpClient();
      Request request = new Request.Builder().url(&quot;http://127.0.0.1:5000/&quot;).build();
      
      okHttpClient.newCall(request).enqueue(new Callback() {
          @Override
          public void onFailure(@NotNull Call call, @NotNull IOException e) {
              TextView textView = findViewById(R.id.textview);
              textView.setText(&quot;network not found&quot;);
          }
  
          @Override
          public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
              TextView textView = findViewById(R.id.textview);
              textView.setText(response.body().string());
          }
      });
  
  
  
  }
  }
  </code></pre>
  <p>Hello.py:</p>
  <pre><code>from flask import Flask
  
  app = Flask(__name__)
  
  @app.route('/')
  def hello_world():
      return 'Hello Ani'
  
  @app.route('/one')
  def hello_one():
      return 'Hello one'
  
  @app.route('/two')
  def hello_two():
      return 'Hello two'
  
  if __name__ == &quot;__main__&quot;:
      app.run(host = '0.0.0.0')
  </code></pre>
  <p>The server is working fine in my network. If I connect to my ipv4 adress followed by the port number 5000, like <a href="http://127.0.0.1:5000/" rel="nofollow noreferrer">http://127.0.0.1:5000/</a> from my browser, or even from my phone's browser, &quot;Hello Ani&quot; appears on the screen, but my app doesn't seem to connect to the server and I can't figure out why.</p>
  <p>I would really appreciate if you could help me solve this issue. Thanks !</p>