我正在尝试将从retrofit2创建的json对象发送到API。 API期望的一些变量是布尔值。无论来自改造客户端的这些变量输入是什么,API中的结果数据都将为空。其他变量绝对可以。当我检查邮递员的相同输入时,值就很好了(对于布尔值和非布尔值变量)。 getter设置程序的名称可能不正确,但我不太确定。如果有人可以帮助,那就太好了。
以下是构建请求:
final AucUserEndPoint apiService3 = APIClient.getPostClient().create(AucUserEndPoint.class);
Call<UpdateAucUser> call = apiService3.updateUser(new UpdateAucUser(userId, acceptTnC, loanType, hasFinancial, hasBankStmt,
requestAmount, isProprietor, noEmployee));
call.enqueue(new Callback<UpdateAucUser>() {
@Override
public void onResponse(Call<UpdateAucUser> call, Response<UpdateAucUser> response) {
if(response.code()==400){
try {
Gson gson = new Gson();
CustomError message=gson.fromJson(response.errorBody().charStream(),CustomError.class);
String errMsg = message.getErrorMessageKey();
System.out.println(errMsg);
} catch (Exception e) {
System.out.println("Unknown errors from UU");
}
} else if (response.isSuccessful()) {
userId = response.body().getUserId();
System.out.println("New Lead updated");
} else {
System.out.println("Response body is now null:UU");
}
}
@Override
public void onFailure(Call<UpdateAucUser> call, Throwable t) {
System.out.println("Some Failure occured");
}
});
这是我在APIClient类中的getPostClient():
public static Retrofit getPostClient() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory
.create()).build();
return retrofit;
}
这是AUCUserEndPOint类:
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
public interface AucUserEndPoint {
@POST("/auctest1-ws/api/pending")
Call<UpdateAucUser> updateUser(@Body UpdateAucUser newUser);
}
最后这是我的UpdateAUCUser的POJO模型
package model;
import com.google.gson.annotations.SerializedName;
public class UpdateAucUser {
@SerializedName("userId")
private String userId;
@SerializedName("acceptTnC")
private String acceptTnC;
@SerializedName("loanType")
private String loanType;
@SerializedName("hasFinancial")
private boolean hasFinancial;
@SerializedName("hasBankStmt")
private boolean hasBankStmt;
@SerializedName("requestAmount")
private String requestAmount;
@SerializedName("isProprietor")
private boolean isProprietor;
@SerializedName("noEmployee")
private String noEmployee;
public UpdateAucUser(String userId, String acceptTnC,String loanType, boolean hasFinancial, boolean hasBankStmt, String requestAmount, boolean isProprietor, String noEmployee) {
this.userId = userId;
this.acceptTnC = acceptTnC;
this.loanType = loanType;
this.hasFinancial = hasFinancial;
this.hasBankStmt = hasBankStmt;
this.requestAmount = requestAmount;
this.isProprietor = isProprietor;
this.noEmployee = noEmployee;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getAcceptTnC() {
return acceptTnC;
}
public void setAcceptTnC(String acceptTnC) {
this.acceptTnC = acceptTnC;
}
public String getLoanType() {
return loanType;
}
public void setLoanType(String loanType) {
this.loanType = loanType;
}
public boolean getHasFinancial() {
return hasFinancial;
}
public void setHasFinancial(boolean hasFinancial) {
this.hasFinancial = hasFinancial;
}
public boolean getHasBankStmt() {
return hasBankStmt;
}
public void setHasBankStmt(boolean hasBankStmt) {
this.hasBankStmt = hasBankStmt;
}
public String getRequestAmount() {
return requestAmount;
}
public void setRequestAmount(String requestAmount) {
this.requestAmount = requestAmount;
}
public boolean getIsProprietor() {
return isProprietor;
}
public void setIsProprietor(boolean isProprietor) {
this.isProprietor = isProprietor;
}
public String getNoEmployee() {
return noEmployee;
}
public void setNoEmployee(String noEmployee) {
this.noEmployee = noEmployee;
}
}
如果输入是UpdateAucUser(“ abc”,“ y”,“ p”,true,true,“ 1000”,false,“ 20”),则api会看到(“ abc”,“ y”,“ p” ,null,null,“ 1000”,null,“ 20”)