Post request with retrofit + android

Asked

Viewed 1,251 times

1

I’m studying android, and I’m trying to use the retrofit library with android. After reading the documentation and seeing several examples in responses here at stackoverflow I was able to send data from a form via POST for a page written in php and receive the return (in format JSON), however I cannot receive all objects, I receive only the first, I can receive all data if use GET, but not if I use POST, someone would know if this is possible using POST, in case you receive several objects, I have only seen examples with GET, follows my code below:

Dependencies:

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

Mainactivity (inside a button):

        Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://192.168.101.36/json/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            ApiService apiService = retrofit.create(ApiService.class);

            Call<User> call = apiService.validateUser(inputEmail.getText().toString(), inputSenha.getText().toString());
            call.enqueue(new Callback<User>() {
                @Override
                public void onResponse(Call<User> call, Response<User> response) {
                    //Verifica se houve a conexão com sucesso ao webservice
                    if (!response.isSuccessful()) {
                        textView.setText("ERROR onResponde: " + response.code());
                    } else {
                        //requisição retona os dados com sucesso
                        String email = response.body().getEmail();
                        String senha = response.body().getPassword();
                        textView.append("email: " + email + " - senha: " + senha + "\n");
                    }
                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {
                    t.printStackTrace();
                    textView.setText(t.getMessage());
                }

            });

Interface:

public interface ApiService {
   @FormUrlEncoded
   @POST("index.php") 
   Call<User> validateUser(
        @Field("username") String username,
        @Field("password") String password
   );
}

User class:

public class User {
private int id;
private String error, error_msg, username, email, password;

public String getError(){
    return this.error;
}

public String getError_msg(){
    return this.error_msg;
}

public int getId(){
    return this.id;
}

public String getUsername(){
    return this.username;
}

public String getPassword(){
    return this.password;
}

public String getEmail(){
    return this.email;
}

}

Index.php:

$email = $_POST['username'];

$senha = $_POST['password'];

echo '
{
"email":"'.$email.'",
"password":"'.$senha.'"
},
{
"email":"'.$email.'",
"password":"'.$senha.'"
},
{
"email":"[email protected]",
"password":"123456"
}
';
  • What do you mean by all the data? You can post an example answer, and point out what you can’t read.

  • See image: http://www.neoprime.com.br/Screenshot_2016-09-26-17-42-10.png In the index.php file there are 3 objects, but retouched only 1. I tried to make a list of the data, but I could not implement the passage of the parameters. Only able to implement with GET, so yes I can return all objects, but I want to pass my data via POST and not via GET. Ex: I want to pass the token via POST and receive the user data in the request.

  • Solved: For future queries, follow the solution link: http://stackoverflow.com/questions/39724751/how-get-list-of-objects-with-post-request-to-retrofit-2-1-0-with-android

  • try this... echo " [{ 'email':'$email', 'password':'$password' }, { 'email':'$email', 'password':'$password' }, { 'email':'[email protected]', 'password':'123456' }] ;";

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.