title: flask + retrofit in kotlin cannot acces API endpoint with IP address (Failed to connect to /127.0.0.1:5000)
body: <p>I created a api in flask which returns longitude, latitude and id. And I want to get that response within my own android device. I tested the api with postman, and I  can access it via phone browser too with <br><code>URL = &quot;http://192.168.0.185:3000&quot;</code></p>
  <p>Trying to access it with retrofit produces me long error track I can't figure out.</p>
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.posttest, PID: 2144
      java.net.ConnectException: Failed to connect to /127.0.0.1:5000
          at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:285)
          at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:195)
          at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:249)
          at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:108)
          at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:76)
          at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:245)
          at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
          at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
          at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
          at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
          at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
          at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
          at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
          at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
          at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197)
          at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:502)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
          at java.lang.Thread.run(Thread.java:919)
       Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 5000) from /127.0.0.1 (port 37738) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
  </code></pre>
  <p>I tried the approach of running the app on my emulator and then trying to access it with <br>
  <code>URL =&quot;http://10.0.2.2:3000&quot;</code></p>
  <p>It still gives me error. How can I achieve that? Here is how my api response looks like:
  <a href="https://i.stack.imgur.com/StQYN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/StQYN.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/66R42.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/66R42.png" alt="enter image description here" /></a></p>
  <p>My Flask server code is very simple</p>
  <pre><code>app = Flask(__name__)
  api = Api(app)
  api.add_resource(LocationValues, '/adamapi')
  
  @app.route('/')
  def hello():
      return 'Hello,REST'
  
  if __name__ == '__main__':
      app.run(&quot;0.0.0.0&quot;,debug=True,port= 3000)
  </code></pre>
  <p><code>Resource.py</code></p>
  <pre><code>    def get(self):
          connection = sqlite3.connect('db.sqlite')
          cursor = connection.cursor()
          query = &quot;SELECT * FROM location WHERE id = 1&quot;
          result = cursor.execute(query).fetchone()
          connection.close()
          if result:
              return {'item': {'measure_id': result[0], 'longitude': result[1], 'latitude': result[2]}}
          return {&quot;message&quot;: &quot;Wrong get request&quot;}, 400
  </code></pre>
  <p>And Kotlin files <br>
  <code>Contants.kt</code></p>
  <pre><code>class Constants {
      companion object{
          const val BASE_URL = &quot;http://192.168.0.185:3000&quot;
      }
  }
  </code></pre>
  <p><code>DataClass.kt </code></p>
  <pre><code>data class Post(
      val item: Item
  )
  
  data class Item(
      val latitude: Double,
      val longitude: Double,
      val measure_id: Int
  )
  </code></pre>
  <p><code>SimpleApi.kt</code></p>
  <pre><code>interface SimpleApi {
      @GET(&quot;adamapi&quot;)
      suspend fun getPost(): Post
  }
  </code></pre>
  <p><code>Repository.kt</code></p>
  <pre><code>class Repository {
      suspend fun getPost(): Post {
          return RetrofitInstance.api.getPost()
  
      }
  }
  </code></pre>
  <p><code>RetrofitInstance.kt</code></p>
  <pre><code>object RetrofitInstance {
  
      private val retrofit  by lazy {
          Retrofit.Builder()
              .baseUrl(BASE_URL)
              .addConverterFactory(GsonConverterFactory.create())
              .build()
  
      }
      val api:SimpleApi by lazy {
          retrofit.create(SimpleApi::class.java)
      }
  }
  </code></pre>
  <p><code>MainViewModel and MainViewModelFactory classes in .kt</code></p>
  <pre><code>class MainViewModel(private  val repository: Repository):ViewModel() {
      val myResponse: MutableLiveData&lt;Post&gt; = MutableLiveData()
      fun getPost (){
          viewModelScope.launch {
              val response :Post = repository.getPost()
              myResponse.value = response
          }
      }
  }
  
  
  class MainViewModelFactory (private val repository:Repository):ViewModelProvider.Factory {
      override fun &lt;T: ViewModel?&gt; create (modelClass: Class&lt;T&gt;):T{
          return MainViewModel(repository) as T
      }
  }
  </code></pre>
  <p>I added internet permission on Manifest file. Also this explanation here wasn't working on my device as well -&gt;
  <a href="https://medium.com/analytics-vidhya/how-to-make-client-android-application-with-flask-for-server-side-8b1d5c55446e" rel="nofollow noreferrer">https://medium.com/analytics-vidhya/how-to-make-client-android-application-with-flask-for-server-side-8b1d5c55446e</a> <br> Both my devices are using Wifi.</p>
  <p>Error:
  <a href="https://i.stack.imgur.com/Xf5LD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xf5LD.png" alt="enter image description here" /></a></p>
  <p>Update: Following @martin-zeitler answer It now works on emulator. But using my ipv4 address still gives errors. <br>
  <a href="https://i.stack.imgur.com/aAN5l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aAN5l.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/ZyIxA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZyIxA.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/hXj19.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hXj19.png" alt="enter image description here" /></a></p>