我正在进行改造,我有一些问题,我不知道如何解决它。我有两种方法来调用我的api。一个是正常工作,这是方法smsPincodeCheck。
当我尝试使用另一个api调用时,我的问题就出现了。当我尝试添加isValidPincode时,这两种方法中的任何一种都在起作用。
我认为问题必须在我的模型类中,因为当我添加带有三个变量的新构造函数和@SerializedName(“pincode”)时,两个调用中的任何一个都不起作用。
smsPincodeCheck向服务器发送两个变量 isValidPincode向服务器发送三个变量
为了能够使用它,我在模型中创建了两个构造函数,一个可以使用两个变量,另一个可以使用三个。我怀疑问题可能出在这个地方,但我不确定如何解决它。
一些帮助将不胜感激。
接口:
@POST("api/check")
Call<SMSPinCode> smsPincodeCheck(@Body User user);
@POST("api/check/code")
Call<ResponseSMS> isValidPincode(@Body User user);
型号:
@Expose
private Long id;
private String name;
private String lastname;
@SerializedName("email")
private String email;
@SerializedName("phoneNumber")
private String telephone;
@SerializedName("pincode")
private String pincode;
public User(String phone, String email){
this.telephone = phone;
this.email = email;
}
public User(String phone, String email, String pincode){
this.telephone = phone;
this.email = email;
this.pincode = pincode;
}
//Getter and setters
我的两种方法: 1)调用isValidPincode:
User user = new User("34649375362", "m@m.m", "50878");
Call<ResponseSMS> call = apiService.isValidPincode(user);
call.enqueue(new Callback<ResponseSMS>() {
@Override
public void onResponse(Call<ResponseSMS> call, Response<ResponseSMS> response) {
if (response.isSuccess()) {
Toast.makeText(RegisterActivity.this, "200", Toast.LENGTH_SHORT).show();
Log.i(TAG, "Pincode Check: " + response.code());
}
if (response.code() == 400) {
Toast.makeText(RegisterActivity.this, "400", Toast.LENGTH_SHORT).show();
Log.i(TAG, "Pincode Check: " + response.code());
}
}
@Override
public void onFailure(Call<ResponseSMS> call, Throwable t) {
Log.i(TAG, call.request().body().toString());
Log.i(TAG, t.getMessage());
}
});
2)smsPinCodeCheck:
user = new User("34649375362", "m@m.m");
Call<ResponseSMS> call = apiService.smsPincodeCheck(user);
progressDialog = ProgressDialog.show(RegisterThreeActivity.this, "", getResources().getString(R.string.loading));
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();
Log.i(TAG, "Pincode Check: " + response.code());
}
if(response.code() == 401){
sendSMS();
Log.i(TAG, "Pincode Check: " + response.code());
}
}
@Override
public void onFailure(Call<ResponseSMS> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(RegisterThreeActivity.this, "Failure check sms", Toast.LENGTH_SHORT).show();
}
});
答案 0 :(得分:0)
启用登录改装API。它会记录发送到服务器的所有内容和响应。
private void enableLogging(OkHttpClient.Builder builder) {
if (builder != null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
// Retrofit2 okhttp client is now a builder
builder.addInterceptor(logging);
}
}
通过这种方式,您可以调试发送到服务器的内容以及缺少的内容。
有关详情:Retrofit Logging
这不是一个真正的答案,但这可以帮助你。
答案 1 :(得分:0)
我已经阅读了您的评论,我认为您的代码工作正常,但您的服务器有问题(可能您有错误的参数或其他内容)。
你有来自改造的这个日志:
{"phoneNumber":"34649375362","email":"m@m.m","pincode":"50878","commerceType":0,"businesSubCategory":0,"businesCategory":0,"active":false,"typeAddress":0}
此json对象对应于此对象User user = new User("34649375362", "m@m.m", "50878");
如果您有此日志表示您的请求已发送到服务器,但在您的onResponse中,您没有100%的案例覆盖率,例如您没有针对404响应的任何操作。
请执行以下操作并检查是否在logcat中打印了最后一个日志:
if (response.isSuccess()) {
Toast.makeText(RegisterActivity.this, "200", Toast.LENGTH_SHORT).show();
Log.i(TAG, "Pincode Check: " + response.code());
} else if (response.code() == 400) {
Toast.makeText(RegisterActivity.this, "400", Toast.LENGTH_SHORT).show();
Log.i(TAG, "Pincode Check: " + response.code());
} else {
Log.i(TAG, "Unexpected error: " + response.code());
}