我是Android编程新手,使用Retrofit。
我看到很多例子来改造库,使GET
请求,传递参数和把所有的对象在一个页面上的PHP
。这是可能的,做同样的事情与POST
?我想把所有的数据(JSON
对象)从我的webservice
,通过POST
传递参数,但我不能做实现。
有没有人做过或有一个例子来帮助我?
研究文档并查看这里的stackoverflow中的示例,我可以制作下面的示例,但只返回第一个对象。
依赖性:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
MainActivity(按钮内部):
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 + " - password: " + senha + "'n");
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
t.printStackTrace();
textView.setText(t.getMessage());
}
});
接口:
public interface ApiService {
@FormUrlEncoded
@POST("index.php")
Call<User> validateUser(
@Field("username") String username,
@Field("password") String password
);
}
类用户:
public class User {
private String username, email, password;
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'];
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")
Call<List<User>> validateUser(
@Field("username") String username,
@Field("password") String password
);
然后在你的回调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());
}
}
}