Remon Shehatta
Remon Shehatta

Reputation: 1470

Failed to connect to localhost/127.0.0.1:5000

I am using Retrofit2 to make network requests but the endpoint is locally on my machine.

So my base URL is private val BASE_URL = "http://localhost:8000/", but it gives me this response Failed to connect to localhost/127.0.0.1:5000

So I did some search and I found out that I need to use my local IP address as the following private val BASE_URL = "http:192.168.1.15//:8000/", but then I get this response Response{protocol=http/1.1, code=404, message=Not Found, url=http://192.168.1.15//:8000/api/Home/RegisterShop}

Any idea how to solve this issue?

Upvotes: 1

Views: 11343

Answers (3)

Maxim
Maxim

Reputation: 476

In my case the local server address was http://127.0.0.1:5000 (I used flask for my server, it runs on port 5000 by default)

Upvotes: 0

FloM89
FloM89

Reputation: 13

Things you should do:

  1. Use your local IP Adress instead of localhost or 127.0.0.1
  2. While local, there is no SSL so you need to put to you manifest:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 

and to <application>:

<application
    android:usesCleartextTraffic="true">

After that it should work... But keep in mind that if you go to prod, CleartextTraffic="true" is not a good idea.

Upvotes: 0

Onik
Onik

Reputation: 19959

If you are using an Android emulator, the IP address of the host machine would be 10.0.2.2.

So in your case it'd be http://10.0.2.2:8000/.

Upvotes: 6

Related Questions