0

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?

Tom Finet
  • 2,056
  • 6
  • 30
  • 54
  • Is your computer on the same network as the phone? Is your Web server set up to respond to the `192.x.y.z` address, or just `localhost`? – CommonsWare Jun 29 '17 at 20:43
  • They are both on the same network. How do I set up my web server to respond to 192.x.y.z address? – Tom Finet Jun 29 '17 at 20:44
  • 1
    Are you using "manage.py runserver" for starting your django app? It binds to localhost only by default and you would need to specify IP address to bind to as parameter ( see this response https://stackoverflow.com/questions/5768797/manage-py-runserver#5768853 ) – Josef Adamcik Jun 29 '17 at 20:45
  • Yes I'm just using python manage.py runserver – Tom Finet Jun 29 '17 at 20:46
  • Thank you so much, it now works! – Tom Finet Jun 30 '17 at 05:49

0 Answers0