Android 回复/响应失败
我是android开发领域的新手,目前正在开发一款应用程序,该应用程序使用改型将数据发送到某个服务器。已发送的数据将保存在服务器上,但不会返回任何响应 我用PostMan试用过,它会返回预期的结果,但从应用程序中试用时,它似乎不会返回任何结果 我正在尝试发送的JSON数据示例Android 回复/响应失败,android,retrofit,Android,Retrofit,我是android开发领域的新手,目前正在开发一款应用程序,该应用程序使用改型将数据发送到某个服务器。已发送的数据将保存在服务器上,但不会返回任何响应 我用PostMan试用过,它会返回预期的结果,但从应用程序中试用时,它似乎不会返回任何结果 我正在尝试发送的JSON数据示例 { "users": [ { "user_id": null, "user_first_name": "Thanos", "u
{
"users": [
{
"user_id": null,
"user_first_name": "Thanos",
"user_last_name": "",
"user_phone": "+1000000000",
"user_photo": "/images/doctorstrange.png",
"user_city": "Titan",
"user_email": "doctorstrange@titan.com",
"user_pass": "iaminevitable",
"user_status": "End Game",
"user_phenotic_name": "The Mad Titan",
"user_country": "Titan",
"country_id": 1,
"registration_date": null,
"gender": "Male"
}
]
}
我期待的回应
{
"status": 81,
"msg": "The User information is saved."
}
响应的POJO类
public class Response {
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("msg")
@Expose
private String msg;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
面向用户的POJO类
public class User {
@SerializedName("user_id")
private Object userId;
@SerializedName("user_first_name")
private String userFirstName;
@SerializedName("user_last_name")
private String userLastName;
@SerializedName("user_phone")
private String userPhone;
@SerializedName("user_photo")
private String userPhoto;
@SerializedName("user_city")
private String userCity;
@SerializedName("user_email")
private String userEmail;
@SerializedName("user_pass")
private String userPass;
@SerializedName("user_status")
private String userStatus;
@SerializedName("user_phenotic_name")
private String userPhenoticName;
@SerializedName("user_country")
private String userCountry;
@SerializedName("country_id")
private Integer countryId;
@SerializedName("registration_date")
private Object registrationDate;
@SerializedName("gender")
private String gender;
public User(Object userId, String userFirstName, String userLastName, String userPhone, String userPhoto, String userCity, String userEmail, String userPass, String userStatus, String userPhenoticName, String userCountry, Integer countryId, Object registrationDate, String gender) {
this.userId = userId;
this.userFirstName = userFirstName;
this.userLastName = userLastName;
this.userPhone = userPhone;
this.userPhoto = userPhoto;
this.userCity = userCity;
this.userEmail = userEmail;
this.userPass = userPass;
this.userStatus = userStatus;
this.userPhenoticName = userPhenoticName;
this.userCountry = userCountry;
this.countryId = countryId;
this.registrationDate = registrationDate;
this.gender = gender;
}
public Object getUserId() {
return userId;
}
public void setUserId(Object userId) {
this.userId = userId;
}
public String getUserFirstName() {
return userFirstName;
}
public void setUserFirstName(String userFirstName) {
this.userFirstName = userFirstName;
}
public String getUserLastName() {
return userLastName;
}
public void setUserLastName(String userLastName) {
this.userLastName = userLastName;
}
public String getUserPhone() {
return userPhone;
}
public void setUserPhone(String userPhone) {
this.userPhone = userPhone;
}
public String getUserPhoto() {
return userPhoto;
}
public void setUserPhoto(String userPhoto) {
this.userPhoto = userPhoto;
}
public String getUserCity() {
return userCity;
}
public void setUserCity(String userCity) {
this.userCity = userCity;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public String getUserStatus() {
return userStatus;
}
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
public String getUserPhenoticName() {
return userPhenoticName;
}
public void setUserPhenoticName(String userPhenoticName) {
this.userPhenoticName = userPhenoticName;
}
public String getUserCountry() {
return userCountry;
}
public void setUserCountry(String userCountry) {
this.userCountry = userCountry;
}
public Integer getCountryId() {
return countryId;
}
public void setCountryId(Integer countryId) {
this.countryId = countryId;
}
public Object getRegistrationDate() {
return registrationDate;
}
public void setRegistrationDate(Object registrationDate) {
this.registrationDate = registrationDate;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
接口类
@Headers({
"Accept: application/json",
"Content-Type: application/json"
})
@POST("addUser.json")
Call<BaseResponse> addUser(@Body UserRequest userRequest);
使用这个,我能够向服务器发送数据,问题是我没有从服务器得到任何响应,我只是得到响应失败。任何人都可以帮助/或看到我可能做错了什么?提前感谢您改型依赖于OkHttp,这样您就可以添加,这样您就可以看到响应的主体从您的服务中返回了什么
根据该日志消息,您可能正在处理一个额外的json字段或缺少的json字段,而
LoggingInterceptor
将公开该字段,以便您可以修复它 改型依赖于OkHttp,这样您就可以添加,这样您就可以看到响应主体从您的服务返回了什么
根据该日志消息,您可能正在处理一个额外的json字段或缺少的json字段,而LoggingInterceptor
将公开该字段,以便您可以修复它
public class UserRequest {
@SerializedName("users")
public List<User> users;
}
signupbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Retrofit - Insert Values that will be sent to the server
UserRequest userRequest = new UserRequest();
ArrayList<User> userList = new ArrayList<>();
User user = new User(
null, // for the id since its an auto-increment field in the server's database
"" + firstName.getText().toString(),
"" + lastName.getText().toString(),
"" + mobileNumber.getText().toString(),
"" + "photo from storage",
"" + city.getText().toString(),
"" + emailAddress.getText().toString(),
"" + password.getText().toString(),
"End Game", // status is sent on default
"" + phoneticName.getText().toString(),
"" + userCountry.getText().toString(),
"" + "countryId", // id is default sent
"" + Constanst.GetCurrentDate(),
"" + gender.getSelectedItem().toString());
userList.add(user);
userRequest.users = userList;
// Using the Retrofit
BaseRestInterface baseRestInterface = ServiceGenerator.createService(BaseRestInterface.class, "" + GlobalVariables.BASE_URL + "users/");
Call<BaseResponse> baseResponseCall = baseRestInterface.User(userRequest);
baseResponseCall.enqueue(new Callback<BaseResponse>() {
@Override
public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
Toast.makeText(MainActivity.this, "ID = " + response.body().getStatus()
+ "\n"
+ response.body().getMsg(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<BaseResponse> call, Throwable t) {
Log.e("response-failure", call.toString());
}
});
}
});
E/response-failure: retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@d6f630d
Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $