gpt4 book ai didi

android - 改造后的 Post 方法永远不会在 OnResponse 或 OnFailure 中触发

转载 作者:行者123 更新时间:2023-11-29 19:25:55 25 4
gpt4 key购买 nike

我正在尝试通过改造来检查服务器中是否存在某些数据。我的问题是从未经历过 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) {

}
});

接口(interface):

 @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();
}
});
}

接口(interface):

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;
}
}

最佳答案

现在您需要制作一个单独的模型类来处理您的网络服务的响应:

public class Response {
public String response;
public int status;
}

并将此类替换为 User在您的改造网络服务调用中。

替换UserResponse在 : 1. 你的smsPincodeCheck ApiInterface的方法|界面。 2. Call<Response> call = apiService.smsPincodeCheck(getPhoneNuber, email); 3.

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) {

}
});

关于android - 改造后的 Post 方法永远不会在 OnResponse 或 OnFailure 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41427617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com