Srijith
Srijith

Reputation: 1715

android- Getting json values using gson and retrofit

Iam using retrofit for http calls and gson for json parsing. I referred these links: a-smart-way-to-use-retrofit and deserializing-inner-class-when-using-gson-and-retrofit. But iam getting retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

This is my json response:

{
    "Document": [
        {
            "Name": "name",
            "Password": "passwrd"
        }
    ]
}

This is my rest api:

@GET("/ObjectTracking/login.php/")
public void getSimpleResponse(@Query("username") String username,
                              @Query("pwd") String password,
                              Callback<SimpleResponseHandler> handlerCallback);

This is my restadapter:

Gson gson = new GsonBuilder()
            .excludeFieldsWithoutExposeAnnotation()
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setEndpoint(Constants.BASE_URL)
            .setConverter(new GsonConverter(gson))
            .build();

MainActivity.class:

simpleRestApi.getSimpleResponse(email, password, new Callback<SimpleResponseHandler>() {
        @Override
        public void success(SimpleResponseHandler simpleResponseHandlers, Response response) {
            Log.e("RETROFIT SUCCESS", response.getBody().toString());
        }

        @Override
        public void failure(RetrofitError error) {
            Log.e("Retrofit error", error.getCause().toString());
        }
    });

SimpleResponseHandler.class:

public class SimpleResponseHandler {

    @Expose
    private List<Credentials> credentialList= new ArrayList<Credentials>();

    public List<Credentials> getCredentials() {
        return credentialList;
    }

    public void setCredentials(List<Credentials> credentialList) {
        this.credentialList = credentialList;
    }

    public class Credentials {

        @Expose
        private String mName;

        @Expose
        private String mPassword;

        public String getName() {
            return mName;
        }

        public void setName(String name) {
            this.mName = name;
        }

        public String getPassword() {
            return mPassword;
        }

        public void setPassword(String pwd) {
            this.mPassword = pwd;
        }
    }
}

Upvotes: 0

Views: 1409

Answers (2)

rahul.ramanujam
rahul.ramanujam

Reputation: 5618

Java object field names should match json tag name or use @SerializedName()

public class Document {

        @Expose @SerializedName("Name")
        private String mName;

        @Expose @SerializedName("Password")
        private String mPassword;
}

Java Response Object

public class SimpleResponse {

    @Expose @SerializedName("Document")
    private List<Document> credentialList= new ArrayList<Document>();

    public List<Document> getCredentials() {
        return credentialList;
    }

    public void setCredentials(List<Document> credentialList) {
        this.credentialList = credentialList;
    }
    }

updated api method

@GET("/ObjectTracking/login.php/")
public void getSimpleResponse(@Query("username") String username,
                              @Query("pwd") String password,
                              Callback<List<SimpleResponse>> handlerCallback);

Upvotes: 1

Fouad Wahabi
Fouad Wahabi

Reputation: 804

I suggest to use : jsonschema2pojo for generating pojo from JSON. Here's the appropriate SimpleResponseHandler class :

public class SimpleResponseHandler {

@SerializedName("Document")
@Expose
private List<Document> document = new ArrayList<Document>();

/**
*
* @return
* The document
*/
public List<Document> getDocument() {
return document;
}

/**
*
* @param document
* The document
*/
public void setDocument(List<Document> document) {
this.document = document;
}

public class Document {

    @Expose
    private String Name;
    @Expose
    private String Password;

    /**
    *
    * @return
    * The Name
    */
    public String getName() {
    return Name;
    }

    /**
    *
    * @param Name
    * The Name
    */
    public void setName(String Name) {
    this.Name = Name;
    }

    /**
    *
    * @return
    * The Password
    */
    public String getPassword() {
    return Password;
    }

    /**
    *
    * @param Password
    * The Password
    */
    public void setPassword(String Password) {
    this.Password = Password;
    }

}

}

Upvotes: 0

Related Questions