2016-05-17 11 views
-2

Dies ist Json-Format:Wie verschiedene JSON-Format analysieren Retrofit mit

{ 
"success": true 
"message": "User Login successfully" 
"response": { 
"userDetails": { 
"firstName": "yoyo" 
"lastName": "ho" 
"gender": "" 
"batch": null 
"userId": 4 
"userType": "Student" 
"bio": null 
"phone": null 
"email": "[email protected]" 
"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 wie das ist, wo ich diese json setzen:

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

Um den Dienst zu treffen i benutzten:

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

}

Wenn i debugge den Code ich fand mein UserModel ist leer, sag mir, wie kann ich den Json mit GsonConverterFactory von Retrofit analysieren.

+0

Ihr JSON-Format ist nicht walid. –

+0

Sind Sie sicher, dass Ihre api/v1/login-Methode Ihre UserModel-Klasse zurückgibt, d. H. Die Informationen, die Sie zum Auffüllen der UserModel-Klasse benötigen? Laut einem früheren Kommentar ist Ihr JSON ungültig und wird nicht Ihrer UserModel-Klasse zugeordnet. –

+0

Okey Sie können Antworten sehen. Ich sage nur, wenn Ihr JSON-Format gültig ist, können Sie Pojo-Klassen wie diese erstellen: http://StackOverflow.com/Questions/37037090/JSONOJECT-TO-JAVA-representation/37037284#37037284 –

Antwort

0

Ich glaube, Ihr JSON-Format ist ungültig, ich schlage vor, es mit einem der vielen verfügbaren Online-Tools zu debuggen. Zum Beispiel: json.org

Hoffe, es hilft: json validation

Sie mehr über das Format hier lesen kann.

1

erstens Ihre JSON als ungültig korrigieren, habe ich mir die Freiheit genommen, es zu beheben:

{ 
    "success":true, 
    "message":"User Login successfully", 
    "response": { 
    "userDetails": { 
     "firstName":"yoyo", 
     "lastName":"ho", 
     "gender":"", 
     "batch":null, 
     "userId":4, 
     "userType":"Student", 
     "bio":null, 
     "phone":null, 
     "email":"[email protected]", 
     "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" 
    } 
} 

Ihr aktualisiertes Modell:

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

Verwenden Sie Ihre vorhandenen Code den Dienst dh LoginService Schnittstelle zu treffen und Factory Klasse

+0

Danke für Sie Vorschlag, ich korrigierte mein JSON-Format nach der Anforderung, jetzt funktioniert es gut. :) – binaryakash