-2

This is Json format:

{
"success": true
"message": "User Login successfully"
"response": {
"userDetails": {
"firstName": "yoyo"
"lastName": "ho"
"gender": ""
"batch": null
"userId": 4
"userType": "Student"
"bio": null
"phone": null
"email": "oopsss@gmail.com"
"country": null
"state": null
"city": null
"pictureUrl": null
"fbLink": null
"twLink": null
"liLink": null
"provider": "EMAIL"
"isVerified": 0
"isActive": 1
}-
"token":    "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOnsidXNlcklkIjo0LCJ1c2VyVHlwZSI6IlN0dWRlbnQifSwiaWF0IjoxNDYzNDAzMzc5LCJleHAiOjUwNjMzOTk3Nzl9.kxhCqR_vtHVHQwaUKj0KhF3OyZEY6T4Kg-XnOfunaZY"
}-
}

My Model is like that, where i put this json:

@Parcel
public class UserModel {
public String firstName;
public String lastName;
public String gender;
public String batch;
public String userId;
public String userType;
public String bio;
public String phone;
public String email;
public String country;
public String state;
public String city;
public String pictureUrl;
public String fbLink;
public String twLink;
public String liLink;
public String provider;
public String token;
}

To hit the service i used this:

public interface LoginService {

@FormUrlEncoded
@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("api/v1/login")
Observable<UserModel> loginUser(
        @Field("email") String email,
        @Field("provider") String provider,
        @Field("password") String pass
);


class Factory {
    public static LoginService create() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(AppConstants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit.create(LoginService.class);
    }
}

}

When i debug the code i found my UserModel is empty, tell me how can i parse the json using GsonConverterFactory of Retrofit.

Community
  • 1
  • 1
binaryakash
  • 124
  • 7
  • Your JSON format is not walid. – Zahidul Islam May 17 '16 at 08:39
  • Are you sure your api/v1/login method returns your UserModel class i.e. the information you need to populate the UserModel class? As per earlier comment, your JSON is invalid and doesn't map to your UserModel class. – Clive Seebregts May 17 '16 at 08:45
  • Okey you can see answers. I just say if your json format is valid you can create Pojo classes like that : http://stackoverflow.com/questions/37037090/jsonoject-to-java-representation/37037284#37037284 – Yasin Kaçmaz May 17 '16 at 10:59
  • Well thanks all, i corrected my JSON format now its working fine. :) – binaryakash Jun 01 '16 at 09:55

2 Answers2

1

Firstly correct your JSON as its invalid, I have taken the liberty to fix it:

{
  "success":true,
  "message":"User Login successfully",
  "response": {
    "userDetails": {
      "firstName":"yoyo",
      "lastName":"ho",
      "gender":"",
      "batch":null,
      "userId":4,
      "userType":"Student",
      "bio":null,
      "phone":null,
      "email":"oopsss@gmail.com",
      "country":null,
      "state":null,
      "city":null,
      "pictureUrl":null,
      "fbLink":null,
      "twLink":null,
      "liLink":null,
      "provider":"EMAIL",
      "isVerified":0,
      "isActive":1
    },
   "token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOnsidXNlcklkIjo0LCJ1c2VyVHlwZSI6IlN0dWRlbnQifSwiaWF0IjoxNDYzNDAzMzc5LCJleHAiOjUwNjMzOTk3Nzl9.kxhCqR_vtHVHQwaUKj0KhF3OyZEY6T4Kg-XnOfunaZY"
  }
}

Your Updated Model:

import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;


public class Response {

  @SerializedName("userDetails")
  @Expose
  private UserDetails userDetails;
  @SerializedName("token")
  @Expose
  private String token;

  public UserDetails getUserDetails() {
    return userDetails;
  }

  public void setUserDetails(UserDetails userDetails) {
    this.userDetails = userDetails;
  }

  public String getToken() {
    return token;
  }


  public void setToken(String token) {
    this.token = token;
  }
}


public class UserDetails {

  @SerializedName("firstName")
  @Expose
  private String firstName;
  @SerializedName("lastName")
  @Expose
  private String lastName;
  @SerializedName("gender")
  @Expose
  private String gender;
  @SerializedName("batch")
  @Expose
  private Object batch;
  @SerializedName("userId")
  @Expose
  private Integer userId;
  @SerializedName("userType")
  @Expose
  private String userType;
  @SerializedName("bio")
  @Expose
  private Object bio;
  @SerializedName("phone")
  @Expose
  private Object phone;
  @SerializedName("email")
  @Expose
  private String email;
  @SerializedName("country")
  @Expose
  private Object country;
  @SerializedName("state")
  @Expose
  private Object state;
  @SerializedName("city")
  @Expose
  private Object city;
  @SerializedName("pictureUrl")
  @Expose
  private Object pictureUrl;
  @SerializedName("fbLink")
  @Expose
  private Object fbLink;
  @SerializedName("twLink")
  @Expose
  private Object twLink;
  @SerializedName("liLink")
  @Expose
  private Object liLink;
  @SerializedName("provider")
  @Expose
  private String provider;
  @SerializedName("isVerified")
  @Expose
  private Integer isVerified;
  @SerializedName("isActive")
  @Expose
  private Integer isActive;

  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 getGender() {
    return gender;
  }


  public void setGender(String gender) {
    this.gender = gender;
  }

  public Object getBatch() {
    return batch;
  }

  public void setBatch(Object batch) {
    this.batch = batch;
  }


  public Integer getUserId() {
    return userId;
  }


  public void setUserId(Integer userId) {
    this.userId = userId;
  }


  public String getUserType() {
    return userType;
  }


  public void setUserType(String userType) {
    this.userType = userType;
  }


  public Object getBio() {
    return bio;
  }


  public void setBio(Object bio) {
    this.bio = bio;
  }


  public Object getPhone() {
    return phone;
  }


  public void setPhone(Object phone) {
    this.phone = phone;
  }


  public String getEmail() {
    return email;
  }


  public void setEmail(String email) {
    this.email = email;
  }

  public Object getCountry() {
    return country;
  }

  public void setCountry(Object country) {
    this.country = country;
  }

  public Object getState() {
    return state;
  }


  public void setState(Object state) {
    this.state = state;
  }

  public Object getCity() {
    return city;
  }

  public void setCity(Object city) {
    this.city = city;
  }

  public Object getPictureUrl() {
    return pictureUrl;
  }


  public void setPictureUrl(Object pictureUrl) {
    this.pictureUrl = pictureUrl;
  }


  public Object getFbLink() {
    return fbLink;
  }


  public void setFbLink(Object fbLink) {
    this.fbLink = fbLink;
  }


  public Object getTwLink() {
    return twLink;
  }


  public void setTwLink(Object twLink) {
    this.twLink = twLink;
  }


  public Object getLiLink() {
    return liLink;
  }


  public void setLiLink(Object liLink) {
    this.liLink = liLink;
  }


  public String getProvider() {
    return provider;
  }


  public void setProvider(String provider) {
    this.provider = provider;
  }


  public Integer getIsVerified() {
    return isVerified;
  }


  public void setIsVerified(Integer isVerified) {
    this.isVerified = isVerified;
  }


  public Integer getIsActive() {
    return isActive;
  }


  public void setIsActive(Integer isActive) {
    this.isActive = isActive;
  }
}

public class UserModel {

  @SerializedName("success")
  @Expose
  private Boolean success;
  @SerializedName("message")
  @Expose
  private String message;
  @SerializedName("response")
  @Expose
  private Response response;


  public Boolean getSuccess() {
    return success;
  }


  public void setSuccess(Boolean success) {
    this.success = success;
  }


  public String getMessage() {
    return message;
  }


  public void setMessage(String message) {
    this.message = message;
  }


  public Response getResponse() {
    return response;
  }


  public void setResponse(Response response) {
    this.response = response;
  }
}

Use your existing code to hit the service i.e. LoginService interface and Factory class

Clive Seebregts
  • 2,004
  • 14
  • 18
0

I believe your JSON format is invalid, I suggest debugging it using one of the many available online tools out there. For instance: json validation

You can read more about the format here: json.org

Hope it helps.

jdandradex
  • 128
  • 6