我正在尝试通过改造来检查服务器中是否存在某些数据。我的问题是,无论是onResponse还是OnFailure。代码永远不会传递这两种方法。我在日志中没有收到任何错误。如果我调试,代码停在这里:call.enqueue(new Callback(),并在下一步中走出方法。
一些帮助将不胜感激
代码:
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<User> call = apiService.smsPincodeCheck(getPhoneNuber, email);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if(response.isSuccess()){
Toast.makeText(RegisterThreeActivity.this, "200", Toast.LENGTH_SHORT).show();
}
if(response.code() == 401){
Toast.makeText(RegisterThreeActivity.this, "401", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
接口:
@FormUrlEncoded
@POST("api/check")
Call<User> smsPincodeCheck(@Field("email") String email, @Field("phoneNumber") String phoneNumber);
用户模型:
public class User implements Serializable {
private final static String TAG = "User";
@SerializedName("user")
@Expose
private Long id;
private Long commerceId;
private String email;
private String name;
private String lastname;
private String dni;
private String companyCharge;
.
.
. Getters and setters
JSON:
这是我想要发送到服务器的json:
{
"phoneNumber" : "444444444",
"email" : "example@example.com"
}
编辑1:
Call<ResponseSMS> call = apiService.smsPincodeCheck(getPhoneNuber, email);
call.enqueue(new Callback<ResponseSMS>() {
@Override
public void onResponse(Call<ResponseSMS> call, Response<ResponseSMS> response) {
if(response.isSuccess()){
Toast.makeText(RegisterThreeActivity.this, "200", Toast.LENGTH_SHORT).show();
}
if(response.code() == 401){
Toast.makeText(RegisterThreeActivity.this, "401", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseSMS> call, Throwable t) {
Toast.makeText(RegisterThreeActivity.this, "Failure", Toast.LENGTH_SHORT).show();
}
});
}
接口:
Call<ResponseSMS> smsPincodeCheck(@Field("email") String email, @Field("phoneNumber") String phoneNumber);
型号:
public class ResponseSMS {
public String response;
public int status;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
答案 0 :(得分:2)
现在,您需要创建一个单独的模型类来处理Web服务的响应:
public class Response {
public String response;
public int status;
}
并在改装的网络服务电话中将此课程替换为User
。
将User
替换为Response
:
1. smsPincodeCheck
ApiInterface
接口的Call<Response> call = apiService.smsPincodeCheck(getPhoneNuber, email);
方法。
2. call.enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call, Response<Response> response) {
if(response.isSuccess()){
Toast.makeText(RegisterThreeActivity.this, "200", Toast.LENGTH_SHORT).show();
}
if(response.code() == 401){
Toast.makeText(RegisterThreeActivity.this, "401", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Response> call, Throwable t) {
}
});
3.
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" type="text/css">
<div class="container">
<ul class="list-inline">
<li><a href="#"><span class="material-icons"></span></a></li>
<li><a href="#"><span class="material-icons"></span></a></li>
</ul>
</div>