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
Why android try connect to localhost/127.0.0.1:3000
but not localhost:3000?
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 (ie, 127.0.0.1 on your development machine)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.