这是Json格式:
{
"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"
}-
}
我的模型就像那样,我放了这个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;
}
要点击我用过的服务:
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);
}
}
}
当我调试代码时,我发现我的UserModel为空,请告诉我如何使用Rezeofit的GsonConverterFactory解析json。
答案 0 :(得分:1)
首先将您的JSON更正为无效,我已经冒昧地解决它:
{
"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"
}
}
您的更新型号:
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;
}
}
使用现有代码点击服务,即LoginService
界面和Factory
类
答案 1 :(得分:0)