0

JSON

{
    "status": "success",
    "message": "User Registered Successfully",
    "data": {
        "name": "pooja",
        "mobile_no": "8211111111",
        "email": "pooja@gmail.com",
        "referal_id": null,
        "password": "$2y$10$AaleHI56",
        "created_at": "2021-01-27T09:21:35.056380Z",
        "updated_at": "2021-01-27T09:21:35.056411Z"
    }
}

i have created class from jsonschema2pojo and have verified many times that the json is valid or not but still i get this error

have created two classes for above json - SignUpResponse, SignUpData

public interface RetrofitServicesSignUp {
    @FormUrlEncoded
    @POST("/api/passenger-register")
    Call<SignUpResponse> savePost(
            @Field("name") String name,
            @Field("mobile_no") String mobile_no,
            @Field("email") String email,
            @Field("password") String password,
            @Field("password_confirm") String password_confirm,
            @Field("referal_id") String referal_id,
            @Field("device_token") String device_token
   // @Field("device_token") String device_token
    );
}

MainActivity is

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_up_layout);    

        mAPISignUpInterfaceService = ApiUtilsSignUp.getAPIService();

    
        username = edtUserName.getText().toString();
        password = edtPassword.getText().toString();
        phoneStringValue = edtPhoneNumber.getText().toString();
        conPassword = edtConfirmPassword.getText().toString();
        email = edtEmail.getText().toString();
        referralcode = edtReferralCode.getText().toString();


          btnSignUp.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                boolean failFlag = false;
                stringPhoneNumberlength = edtPhoneNumber.getText().toString().length();   

         



 if (edtUserName.getText().toString().trim().length() == 0) {
                failFlag = true;
                edtUserName.setError("Fill name");
            }
                   //As above have done checking with empty field
                    if (failFlag == false) {
                        //none of the edit text is empty so good to go
                        sendPost(username,phoneStringValue,email,password,conPassword,referralcode);
    
                    }
                }   
              

public void sendPost(String namee, String mobilenumberba, String emaill,String passwordd, String confirmpasswordd,String referalcodee ) {
                  mAPISignUpInterfaceService.savePost(namee,mobilenumberba,emaill,passwordd,confirmpasswordd,referalcodee,"abcdef").enqueue(new Callback<SignUpResponse>() {
                      @Override
                      public void onResponse(Call<SignUpResponse> call, Response<SignUpResponse> response) {

                      if(response.isSuccessful()) {
                          SignUpResponse jsonSignUpResponse = response.body();
                          Log.i(TAG, "Token from JsonResponse: " + jsonSignUpResponse.getStatus());
                          Log.i(TAG, "Token from JsonResponse: " + jsonSignUpResponse.getMessage());
                      }
                  }
                  @Override
                  public void onFailure(Call<SignUpResponse> call, Throwable t) {
                      Log.e(TAG, "Unable to submit post to API."+t.getCause());
                  }
              });
          }
    });
}

}

SignUpData

   public class SignUpData {
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("mobile_no")
        @Expose
        private String mobileNo;
        @SerializedName("email")
        @Expose
        private String email;
        @SerializedName("referal_id")
        @Expose
        private Object referalId;
        @SerializedName("password")
        @Expose
        private String password;
        @SerializedName("created_at")
        @Expose
        private String createdAt;
        @SerializedName("updated_at")
        @Expose
        private String updatedAt;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getMobileNo() {
            return mobileNo;
        }
    
        public void setMobileNo(String mobileNo) {
            this.mobileNo = mobileNo;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public Object getReferalId() {
            return referalId;
        }
    
        public void setReferalId(Object referalId) {
            this.referalId = referalId;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getCreatedAt() {
            return createdAt;
        }
    
        public void setCreatedAt(String createdAt) {
            this.createdAt = createdAt;
        }
    
        public String getUpdatedAt() {
            return updatedAt;
        }
    
        public void setUpdatedAt(String updatedAt) {
            this.updatedAt = updatedAt;
        }
    
    }

SignUpResponse

public class SignUpResponse {
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("data")
    @Expose
    private SignUpData data;

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

    public SignUpData getData() {
        return data;
    }

    public void setData(SignUpData data) {
        this.data = data;
    }

}

so now json is correct then why this error and i have also refered this "Expected BEGIN_OBJECT but was STRING at line 1 column 1" and checked whether json is valid and have tried clean and rebuild option in android studio

bhaju
  • 3
  • 2
  • where is `SignUpResponse, SignUpData` ? – a_local_nobody Jan 27 '21 at 09:59
  • `and checked whether json is valid and have tried clean and rebuild option in android studio` this won't help, because your parsing is wrong, most likely – a_local_nobody Jan 27 '21 at 10:00
  • I think your method savePost in the callback Response response you are expecting a SignUpResponse but the callback is getting a SingUpData, so I think the main problem is that you are trying to convert the json into a SingUpResponse when you are getting a different json. – Juanjo Berenguer Jan 27 '21 at 10:45
  • tried it but stil same – bhaju Jan 27 '21 at 13:55

0 Answers0