我正在将PatientInfo()类对象发送到服务器,但是没有从服务器得到任何响应。 “ response.body()”为空。但是我在logcat中得到了JSON格式。
我的PatientInfo()模型类---
public class PatientInfo implements Serializable {
@SerializedName("id")
@Expose
private String id;
@SerializedName("patient_id")
@Expose
private String patientId;
@SerializedName("firstname")
@Expose
private String firstname;
@SerializedName("lastname")
@Expose
private String lastname;
@SerializedName("email")
@Expose
private String email;
@SerializedName("password")
@Expose
private String password;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("mobile")
@Expose
private String mobile;
@SerializedName("address")
@Expose
private String address;
@SerializedName("sex")
@Expose
private String sex;
@SerializedName("blood_group")
@Expose
private String bloodGroup;
@SerializedName("date_of_birth")
@Expose
private String dateOfBirth;
@SerializedName("affliate")
@Expose
private Object affliate;
@SerializedName("picture")
@Expose
private String picture;
@SerializedName("created_by")
@Expose
private String createdBy;
@SerializedName("create_date")
@Expose
private String createDate;
@SerializedName("status")
@Expose
private String status;
@SerializedName("user_role")
@Expose
private String userRole;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPatientId() {
return patientId;
}
public void setPatientId(String patientId) {
this.patientId = patientId;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Object getAffliate() {
return affliate;
}
public void setAffliate(Object affliate) {
this.affliate = affliate;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getUserRole() {
return userRole;
}
public void setUserRole(String userRole) {
this.userRole = userRole;
}
@Override
public String toString() {
return "PatientInfo{" +
"id='" + id + '\'' +
", patientId='" + patientId + '\'' +
", firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", mobile='" + mobile + '\'' +
", address='" + address + '\'' +
", sex='" + sex + '\'' +
", bloodGroup='" + bloodGroup + '\'' +
", dateOfBirth='" + dateOfBirth + '\'' +
", affliate=" + affliate +
", picture='" + picture + '\'' +
", createdBy='" + createdBy + '\'' +
", createDate='" + createDate + '\'' +
", status='" + status + '\'' +
", userRole='" + userRole + '\'' +
'}';
}
}
我的RegResponse()类-
public class RegResponse implements Serializable {
@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("user_data")
@Expose
private List<PatientInfo> patientInfoList;
public List<PatientInfo> getPatientInfoList() {
return patientInfoList;
}
public void setPatientInfoList(List<PatientInfo> patientInfoList) {
this.patientInfoList = patientInfoList;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "RegResponse{" +
"status='" + status + '\'' +
", message='" + message + '\'' +
", patientInfoList=" + patientInfoList +
'}';
}
}
我的RegSuccessResponse()类----
public class RegSuccessResponse implements Serializable {
@SerializedName("response")
@Expose
private RegResponse response;
public RegResponse getResponse() {
return response;
}
public void setResponse(RegResponse response) {
this.response = response;
}
@Override
public String toString() {
return "RegSuccessResponse{" +
"response=" + response +
'}';
}
}
这是我的Api界面---
public interface HMSAPIService {@POST("auth/registration")
Call<RegSuccessResponse> doReg(@Body PatientInfo patientInfo);}
最后,这是我的RegistrationActivity()代码-
String firstName = metFirstName.getText().toString().trim();
String lastName = metLastName.getText().toString().trim();
String email = metregEmail.getText().toString().trim();
String password = metRegPassword.getText().toString().trim();
String phoneNumber = metPhoneNumber.getText().toString().trim();
String mobileNumber = metMobileNumber.getText().toString().trim();
String bloodGroup = spBloodGroup.getSelectedItem().toString().trim();
String gender = spGender.getSelectedItem().toString().trim();
String dob = metDob.getText().toString().trim();
String path = tvPicName.getText().toString().trim();
String address = metAddress.getText().toString().trim();
patientInfo.setFirstname(firstName);
patientInfo.setLastname(lastName);
patientInfo.setEmail(email);
patientInfo.setPassword(password);
patientInfo.setPhone(phoneNumber);
patientInfo.setMobile(mobileNumber);a
if(spBloodGroup.getSelectedItem().toString().equals("-Select One-")){
patientInfo.setBloodGroup(null);
} else {
patientInfo.setBloodGroup(bloodGroup);
}
if(spGender.getSelectedItem().toString().equals("-Select One-")){
patientInfo.setSex(null);
} else {
patientInfo.setSex(gender);
}
patientInfo.setDateOfBirth(dob);
patientInfo.setPicture(path);
patientInfo.setAddress(address);
if (regSuccessResponse.getResponse() != null) {
if (regSuccessResponse.getResponse().getPatientInfoList() != null) {
regSuccessResponse.getResponse().getPatientInfoList().add(patientInfo);
}
}
Gson gson = new Gson();
String patientInfoobjectstring = gson.toJson(patientInfo);
Log.e(TAG, patientInfoobjectstring);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SharedPref.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
hmsapiService = retrofit.create(HMSAPIService.class);
Call<RegSuccessResponse> call = hmsapiService.doReg(patientInfo);
call.enqueue(new Callback<RegSuccessResponse>() {
@Override
public void onResponse(Call<RegSuccessResponse> call, Response<RegSuccessResponse> response) {
regSuccessResponse = response.body();
if (response.body() != null){
if (response.body().getResponse().getStatus().equals("ok")) {
Toast.makeText(RegistrationActivity.this, "Registration successful!!!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegistrationActivity.this, MainActivity.class).putExtra("go_to", "patient_info"));
progressDialog.dismiss();
Log.d(TAG, regSuccessResponse.getResponse().getMessage());
finish();
} else {
Toast.makeText(RegistrationActivity.this, "Status not found.......", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(RegistrationActivity.this, "Check your connection!!!!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<RegSuccessResponse> call, Throwable t) {
Log.d(TAG, "onFailure: ");
}
});
}
else {
Snackbar.make(findViewById(R.id.reg_linear_layout), R.string.all_data_providing_message, Snackbar.LENGTH_LONG).show();
}
});
//我的服务器响应将是
{"status":"error","message":"There have no data!"}
我的日志显示---
2018-11-29 17:53:30.329 23861-23861/com.example.bdtask.bdtaskhms E/RegistrationActivity:{"address":"Dhaka","blood_group":"O+","date_of_birth":"29/11/2018","email":"shaon054@gmail.com","firstname":"Shaon","lastname":"acharjee","mobile":"494518","password":"sgshsuvs","phone":"4728151","picture":"/storage/emulated/0/demonuts/1543492405451.jpg","sex":"Male"}2018-11-29 17:53:39.194 23861-23861/com.example.bdtask.bdtaskhms E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bdtask.bdtaskhms, PID: 23861
java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.bdtask.bdtaskhms.models.RegResponse com.example.bdtask.bdtaskhms.responses.RegSuccessResponse.getResponse()' on a null object reference
at com.example.bdtask.bdtaskhms.activities.RegistrationActivity.lambda$onCreate$0(RegistrationActivity.java:259)
at com.example.bdtask.bdtaskhms.activities.-$$Lambda$RegistrationActivity$CvYkDzuJ5whfAnUqX8zPIFxXXqo.onClick(lambda)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11159)
at android.view.View$PerformClick.run(View.java:23752)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)