3

i'm having issues getting Retrofit 2.0 to send POST requests to Python-Django.

Here's my Retrofit method.

   public void sendNetworkRequest(User user) {
    //Cria instância retrofit
    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://127.0.0.1:8000/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    UserService services = retrofit.create(UserService.class);
    Call<User> call = services.createAccount(user);

    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            Toast.makeText(CadastrarActivity.this, "Você foi cadastrado com sucesso!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            CheckConnection checkConnection = new CheckConnection();
            if (checkConnection.equals(null)){
                Toast.makeText(CadastrarActivity.this, "Conecte-se a internet!", Toast.LENGTH_SHORT).show();
            }
             else {
                Log.e("heurys", t.getMessage());
                System.out.println(t.getStackTrace());
                Toast.makeText(CadastrarActivity.this, "Algo deu errado!", Toast.LENGTH_SHORT).show();
            }
        }
    });
} 

Here's my interface method used in the Rest call:

public interface UserService {
@POST("rest/cadastro")
Call<User> createAccount(@Body User user);

}

And here's my traceback error:

04-03 12:58:43.726 18692-18692/com.example.ccyrobuosi.estudos E/heurys: Failed to connect to /127.0.0.1:8000

In advance, my Python code works just fine, i used Postman to test it, and its getting the requests properly.

Cyro Buosi
  • 41
  • 2
  • check that you have added the Internet permission to your Manifest as detailed in this question: http://stackoverflow.com/questions/2169294/how-to-add-manifest-permission-to-android-application or check that you are using the localhost from your emulator correctly as pointed out in this questions: http://stackoverflow.com/questions/2234742/test-the-localhost-in-android-emulator?rq=1 – Enrique Saez Apr 03 '17 at 17:37
  • I did gave the internet permission and the other link you sent me said i should use the host of 10.0.2.2, i tried this before, he actually sends the post request but not to my Django server which is 127.0.0.1:8000/ so i don't get any POST requests – Cyro Buosi Apr 03 '17 at 17:55
  • sorry at this point I am not sure how to help you anymore – Enrique Saez Apr 03 '17 at 19:36

0 Answers0