Php 如何通过post请求获取对象列表,以便使用android改造2.1.0

Php 如何通过post请求获取对象列表,以便使用android改造2.1.0,php,android,post,request,retrofit,Php,Android,Post,Request,Retrofit,我对Android编程和改型工作还不熟悉 我看到了许多改进库的示例,包括发出GET请求、传递参数以及将所有对象放在PHP中的页面上。 可以对POST执行相同的操作吗? 我想从我的webservice中获取所有数据(JSON对象),通过POST传递参数,但我无法实现 有没有人做过或有一个例子来帮助我 研究文档并查看stackoverflow中的示例,我可以制作下面的示例,但只返回第一个对象 依赖项: compile 'com.squareup.retrofit2:retrofit:2.1.0' c

我对Android编程和改型工作还不熟悉

我看到了许多改进库的示例,包括发出
GET
请求、传递参数以及将所有对象放在
PHP
中的页面上。 可以对
POST
执行相同的操作吗? 我想从我的
webservice
中获取所有数据(
JSON
对象),通过
POST
传递参数,但我无法实现

有没有人做过或有一个例子来帮助我

研究文档并查看stackoverflow中的示例,我可以制作下面的示例,但只返回第一个对象

依赖项:

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
MainActivity(按钮内):

Index.php:

$email = $_POST['username'];
$senha = $_POST['password'];

if ($email == "manoelps@live.com") {

  echo '
  {
    "email":"' . $email . '",
    "password":"' . $senha . '"
  },
  {
    "email":"myemail@live.com",
    "password":"654321"
  },
  {
    "email":"joselito@joselito.com",
    "password":"123456"
  }
  ';

} else {

  echo '
  {
    "email":"otheremail@otheremail.com",
    "password":"987654"
  },
  {
    "email":"otheremail@otheremail.com",
    "password":"987654"
  },
  {
    "email":"otheremail@otheremail.com",
    "password":"987654"
  }
  ';
}
如果我在应用程序中使用作为对象数组出现错误:

[{
"email":"manoelps@live.com",
"password":"123456"
},
{
"email":"manoelps@live.com",
"password":"123456"
}]

如果期望多次返回,则需要指定调用返回列表

   @FormUrlEncoded
   @POST("index.php") 
   Call<User> validateUser(
        @Field("username") String username,
        @Field("password") String password
   );
@FormUrlEncoded
@POST(“index.php”)
呼叫验证者(
@字段(“用户名”)字符串用户名,
@字段(“密码”)字符串密码
);
应该是

   @FormUrlEncoded
   @POST("index.php") 
   Call<List<User>> validateUser(
        @Field("username") String username,
        @Field("password") String password
   );
@FormUrlEncoded
@POST(“index.php”)
呼叫验证者(
@字段(“用户名”)字符串用户名,
@字段(“密码”)字符串密码
);

然后,在回调
response.body()
中,将为您提供一个可以迭代的用户对象列表。

如果需要多次返回,则需要指定您的调用返回一个列表

   @FormUrlEncoded
   @POST("index.php") 
   Call<User> validateUser(
        @Field("username") String username,
        @Field("password") String password
   );
@FormUrlEncoded
@POST(“index.php”)
呼叫验证者(
@字段(“用户名”)字符串用户名,
@字段(“密码”)字符串密码
);
应该是

   @FormUrlEncoded
   @POST("index.php") 
   Call<List<User>> validateUser(
        @Field("username") String username,
        @Field("password") String password
   );
@FormUrlEncoded
@POST(“index.php”)
呼叫验证者(
@字段(“用户名”)字符串用户名,
@字段(“密码”)字符串密码
);

然后在您的回调中
response.body()

        Call<List<User>> call = apiService.validateUser(inputEmail.getText().toString(), inputSenha.getText().toString());
        call.enqueue(new Callback<java.util.List<User>>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {
                //Verifica se houve a conexão com sucesso ao webservice
                if (!response.isSuccessful()) {
                    textView.setText("ERROR onResponde: " + response.code());
                } else {

                    try {

                        //pegando os dados
                        List<User> listUsers = response.body();

                        //retona os dados
                        for (User c : listUsers) {
                            textView.append("Email: " + c.getEmail() + " - " + "Senha: " + c.getPassword() + "\n");
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        textView.setText("ERROR:" + e.getMessage());
                    }
                }
            }
Call Call=apiService.validateUser(inputEmail.getText().toString(),inputSenha.getText().toString());
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
//验证是否有一个成功的网络服务
如果(!response.issusccessful()){
setText(“错误onResponde:+response.code());
}否则{
试一试{
//佩甘多斯护墙板
List listUsers=response.body();
//雷托纳护墙板
for(用户c:listUsers){
textView.append(“电子邮件:+c.getEmail()+”-“+”Senha:+c.getPassword()+”\n”);
}
}捕获(例外e){
e、 printStackTrace();
setText(“错误:+e.getMessage());
}
}
}

为了将来的参考,对于需要它的人,我的回调如下所示:

        Call<List<User>> call = apiService.validateUser(inputEmail.getText().toString(), inputSenha.getText().toString());
        call.enqueue(new Callback<java.util.List<User>>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {
                //Verifica se houve a conexão com sucesso ao webservice
                if (!response.isSuccessful()) {
                    textView.setText("ERROR onResponde: " + response.code());
                } else {

                    try {

                        //pegando os dados
                        List<User> listUsers = response.body();

                        //retona os dados
                        for (User c : listUsers) {
                            textView.append("Email: " + c.getEmail() + " - " + "Senha: " + c.getPassword() + "\n");
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        textView.setText("ERROR:" + e.getMessage());
                    }
                }
            }
Call Call=apiService.validateUser(inputEmail.getText().toString(),inputSenha.getText().toString());
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
//验证是否有一个成功的网络服务
如果(!response.issusccessful()){
setText(“错误onResponde:+response.code());
}否则{
试一试{
//佩甘多斯护墙板
List listUsers=response.body();
//雷托纳护墙板
for(用户c:listUsers){
textView.append(“电子邮件:+c.getEmail()+”-“+”Senha:+c.getPassword()+”\n”);
}
}捕获(例外e){
e、 printStackTrace();
setText(“错误:+e.getMessage());
}
}
}

Ben,亲爱的,非常非常感谢!它100%起作用了,这一周很痛苦。Ben,亲爱的,非常感谢!它100%起作用了,这一周很痛苦。