I am writing a backend api for my android client application using the django rest framework. The api, because it is still in development, is being ran locally on my computer. The android application is being ran on a physical sony xperia z. I want to be able to make calls to the api endpoints from the physical device despite the api being ran locally.
I have tried using the IPv4 address of my computer in the base api url like so:
This "http://127.XX.X.X:8000/" instead of this "http://127.0.0.1:8000/".
I have tried using the IP address of the used LAN:
This "http://192.XXX.X.XXX:8000/" instead of this "http://127.0.0.1:8000/".
None of which make calls to the api endpoints.
Perhaps the error lies in my android client, in the code which is responsible for making requests to the endpoints.
I use the retrofit api to make calls to the android app backend api.
Here is my UserService interface:
public interface UserService {
@POST("users/create/")
Call<User> createUser(@Body User user);
@POST("users/api-token-auth/")
Call<String> loginInToken(@Body String email, @Body String password);
}
Here is my user repository which uses this interface to make calls to the api:
public class UserRepository implements UserRepositoryInterface {
private UserService userServiceApi;
public UserRepository() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
userServiceApi = retrofit.create(UserService.class);
}
@Override
public void createUser(User user) {
Call<User> call = userServiceApi.createUser(user);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
Log.d("USER_REPOSITORY", response.toString());
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Log.d("USER_REPOSITORY", t.toString());
}
});
}
}
I am focusing on getting the create user endpoint working. However, when I run the app on my physical device, the throwable is printed to the log in the onFailure interface method saying that USER_REPOSITORY: java.net.ConnectException: Failed to connect to /XXX.XX.X.X:8000
no matter the IP address in the place of the X's.
Just in case, here are the urls that I am calling in the urls.py file in the django-rest api, to show that I am calling the correct urls:
url(r'^api-token-auth/', auth_views.obtain_auth_token),
url(r'create/$', user_views.UserCreate.as_view(), name="create"),
There must be a way to call the api endpoints when the api isn't in production. What am I doing wrong or what am I not doing?