0

I'm trying connect to localhost:3000/full with Retrofit on Android.

public class RestManager {

private QaraSozService qaraSozService;

public QaraSozService getQaraSozService() {


    if (qaraSozService == null) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://localhost:3000")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        qaraSozService = retrofit.create(QaraSozService.class);
    }

    return qaraSozService;
}
}
public interface QaraSozService {

@GET("/full")
Call<List<QaraSoz>> getAllQaraSoz();
}

But i get onFailure when i try connect, i get error message i get Failed to connect to localhost/127.0.0.1:3000 enter image description here

Why android try connect to localhost/127.0.0.1:3000 but not localhost:3000?

Dossanov
  • 121
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [How to connect to my http://localhost web server from Android Emulator in Eclipse](https://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator-in-eclips) – Jaydip Kalkani Nov 05 '18 at 11:09

2 Answers2

0

You API server run on desktop/laptop, and if you try to connect to this api from mobile device, you should set ip of you desktop/laptop in baseUrl instead localhost (something like this: baseUrl("http://192.168.0.1:3000"))

Be sure that you server and you mobile device in same local network.

Or, if you use android emulator instead real device, you should use ip 10.0.2.2. More info here: https://developer.android.com/studio/run/emulator-networking

10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)

igor_rb
  • 1,821
  • 1
  • 19
  • 34
0

I see it is Ubuntu so do the following steps:- -From terminal type >> ifconfig This will print out the your machine wlan ip address This ip which you will use instead of 127.0.0.1 so from your browser -you can test it by typing wlanip:3000 this should be equivalent to localhost:3000. -If you are facing in issue you may need to disable Ubuntu fire wall from terminal >> sudo ufw disable - ensure that your Android device and your Ubuntu machine are connected to same network then test your app after replacing 127.0.0.1 in your Android code to the one you got from ifconfig command

Ramy Ibrahim
  • 656
  • 4
  • 19