How parcelable custom arraylist? - java

I have two classes which implement Parcelable. And objects of one of them I have to write to a list. First model:
public class Challenge implements Parcelable {
private long id;
#JsonProperty("max_volume")
private int maxVolume;
#JsonProperty("current_volume")
private int currentVolume;
private String name;
private String description;
#JsonProperty("max_count")
private int maxCount;
#JsonProperty("date_from")
private Date dateFrom;
#JsonProperty("date_to")
private Date dateTo;
#JsonProperty("create_time")
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ZZZZ")
private Date createTime;
private ChallengeStatus status;
#JsonProperty("update_date")
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ZZZZ")
private Date updateTime;
private ChallengeCategory category;
private Subject subject;
private ArrayList<User> userArrayList;
private int practice;
public double getProgress() {
return progress;
}
public void setProgress(double progress) {
this.progress = progress;
}
public int getPractice() {
return practice;
}
public void setPractice(int practice) {
this.practice = practice;
}
private double progress;
public Challenge() {
}
private Creator<User> creator;
protected Challenge(Parcel in) {
id = in.readLong();
maxVolume = in.readInt();
currentVolume = in.readInt();
name = in.readString();
description = in.readString();
maxCount = in.readInt();
// dateFrom = new Date(in.readLong());
// dateTo = new Date(in.readLong());
// createTime = new Date(in.readLong());
// status = ChallengeStatus.getEnum(in.readString());
// updateTime = new Date(in.readLong());
// category = ChallengeCategory.getEnum(in.readString());
subject = in.readParcelable(Subject.class.getClassLoader());
userArrayList = in.readTypedList(userArrayList, User.CREATOR);
}
public static final Creator<Challenge> CREATOR = new Creator<Challenge>() {
#Override
public Challenge createFromParcel(Parcel in) {
return new Challenge(in);
}
#Override
public Challenge[] newArray(int size) {
return new Challenge[size];
}
};
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getMaxVolume() {
return maxVolume;
}
public void setMaxVolume(int maxVolume) {
this.maxVolume = maxVolume;
}
public int getCurrentVolume() {
return currentVolume;
}
public void setCurrentVolume(int currentVolume) {
this.currentVolume = currentVolume;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getMaxCount() {
return maxCount;
}
public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}
public Date getDateFrom() {
return dateFrom;
}
public void setDateFrom(Date dateFrom) {
this.dateFrom = dateFrom;
}
public Date getDateTo() {
return dateTo;
}
public void setDateTo(Date dateTo) {
this.dateTo = dateTo;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public ChallengeStatus getStatus() {
return status;
}
public void setStatus(ChallengeStatus status) {
this.status = status;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public ChallengeCategory getCategory() {
return category;
}
public void setCategory(ChallengeCategory category) {
this.category = category;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
#Override
public int describeContents() {
return 0;
}
public ArrayList<User> getUserArrayList() {
return userArrayList;
}
public void setUserArrayList(ArrayList<User> userArrayList) {
this.userArrayList = userArrayList;
}
public static Creator<Challenge> getCREATOR() {
return CREATOR;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeInt(maxVolume);
dest.writeInt(currentVolume);
dest.writeString(name);
dest.writeString(description);
dest.writeInt(maxCount);
// dest.writeLong(dateFrom.getTime());
// dest.writeLong(dateTo.getTime());
// dest.writeLong(createTime.getTime());
// dest.writeString(status.toString());
// dest.writeLong(updateTime.getTime());
// dest.writeString(category.toString());
dest.writeParcelable(subject, flags);
dest.writeTypedList(userArrayList);
}
}
And I have a problem at this line: userArrayList = in.readTypedList(userArrayList, User.CREATOR); It looks like this: And example of the second model:
public class User implements Parcelable {
long id;
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ZZZZ")
Date time;
String image_src;
String first_name;
String last_name;
public User() {
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getImage_src() {
return image_src;
}
public void setImage_src(String image_src) {
this.image_src = image_src;
}
protected User(Parcel parcel) {
id = parcel.readLong();
time = new Date(parcel.readLong());
image_src = parcel.readString();
first_name = parcel.readString();
last_name = parcel.readString();
}
public static final Creator<User> CREATOR = new Creator<User>() {
#Override
public User createFromParcel(Parcel in) {
return new User(in);
}
#Override
public User[] newArray(int size) {
return new User[size];
}
};
public static Creator<User> getCREATOR() {
return CREATOR;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeLong(id);
// parcel.writeLong(time.getTime());
parcel.writeString(image_src);
parcel.writeString(first_name);
parcel.writeString(last_name);
}
}
Can you help me to find my mistake?

You must initialized userArrayList before use it:
userArrayList = new ArrayList<User>();
in.readTypedList(userArrayList, User.CREATOR);

Initialize ArraList and retrieve the data to this list from parcelable.
Something like this:
userArrayList = new ArrayList<User>();
in.readTypedList(userArrayList, User.CREATOR);

Related

Parcelable Implementation for Custom Objects

I have 3 POJO types: a Recipe, which contains ingredients, and then the ingredients contain steps. Below is my setup, I am trying to implement Parcelable and cannot determine the appropriate read and write methods:
Recipe.Java:
public class Recipe implements Parcelable {
protected List<Ingredients> ingredients;
private String id;
private String servings;
private String name;
private String image;
private List<Steps> steps;
protected Recipe(Parcel in) {
in.createTypedArray(CREATOR.createFromParcel(Ingredients));
in.createTypedArray(Ingredients.);
id = in.readString();
servings = in.readString();
name = in.readString();
image = in.readString();
steps = in.readArrayList(ClassLoader.getSystemClassLoader());
}
public static final Creator<Ingredients> CREATOR = new Creator<Ingredients>() {
#Override
public Ingredients createFromParcel(Parcel parcel) {
return new Ingredients(parcel);
}
#Override
public Ingredients[] newArray(int i) {
return new Ingredients[0];
}
}
public static final Creator<Recipe> CREATOR = new Creator<Recipe>() {
#Override
public Recipe createFromParcel(Parcel in) {
return new Recipe(in);
}
#Override
public Recipe[] newArray(int size) {
return new Recipe[size];
}
};
public List<Ingredients> getIngredients() {
return ingredients;
}
public void setIngredients(List<Ingredients> ingredients) {
this.ingredients = ingredients;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getServings() {
return servings;
}
public void setServings(String servings) {
this.servings = servings;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public List<Steps> getSteps() {
return steps;
}
public void setSteps(List<Steps> steps) {
this.steps = steps;
}
#Override
public String toString() {
return "ClassPojo [ingredients = " + ingredients + ", id = " + id + ", servings = " + servings + ", name = " + name + ", image = " + image + ", steps = " + steps + "]";
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(id);
parcel.writeString(servings);
parcel.writeString(name);
parcel.writeString(image);
}
}
Ingredients.Java:
public class Ingredients {
private String measure;
private String ingredient;
private String quantity;
public Ingredients(Parcel parcel) {
}
public String getMeasure ()
{
return measure;
}
public void setMeasure (String measure)
{
this.measure = measure;
}
public String getIngredient ()
{
return ingredient;
}
public void setIngredient (String ingredient)
{
this.ingredient = ingredient;
}
public String getQuantity ()
{
return quantity;
}
public void setQuantity (String quantity)
{
this.quantity = quantity;
}
#Override
public String toString()
{
return "ClassPojo [measure = "+measure+", ingredient = "+ingredient+", quantity = "+quantity+"]";
}
}
Steps.Java:
public class Steps {
private String id;
private String shortDescription;
private String description;
private String videoURL;
private String thumbnailURL;
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getShortDescription ()
{
return shortDescription;
}
public void setShortDescription (String shortDescription)
{
this.shortDescription = shortDescription;
}
public String getDescription ()
{
return description;
}
public void setDescription (String description)
{
this.description = description;
}
public String getVideoURL ()
{
return videoURL;
}
public void setVideoURL (String videoURL)
{
this.videoURL = videoURL;
}
public String getThumbnailURL ()
{
return thumbnailURL;
}
public void setThumbnailURL (String thumbnailURL)
{
this.thumbnailURL = thumbnailURL;
}
#Override
public String toString()
{
return "ClassPojo [id = "+id+", shortDescription = "+shortDescription+", description = "+description+", videoURL = "+videoURL+", thumbnailURL = "+thumbnailURL+"]";
}
}
I suggest you to use third party library such as Paperparcel to reduce boilerplate.
Sample of usage:
#PaperParcel // (1)
public final class User implements Parcelable {
public static final Creator<User> CREATOR = PaperParcelUser.CREATOR; // (2)
long id; // (3)
String firstName; // (3)
String lastName; // (3)
#Override public int describeContents() {
return 0;
}
#Override public void writeToParcel(Parcel dest, int flags) {
PaperParcelUser.writeToParcel(this, dest, flags); // (4)
}
}

null response usin Retrofit Library in android

Here is my JSONArray Response from Web service:
And Class Java :
public class Product {
private int id,price,discount;
private String name,image,description,discount_type,discount_exp;
#SerializedName("products")
private List<Product> products;
public Product()
{
}
}
response is null
Your POJO class is wrong
Try this
public class Products_Main
{
#SerializedName("current_page")
int current_page;
#SerializedName("data")
private List<Product> products;
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
and
class Product {
#SerializedName("id")
private int id;
#SerializedName("price")
int price;
#SerializedName("discount")
int discount;
#SerializedName("name")
private String name;
#SerializedName("image")
private String image;
#SerializedName("description")
private String description;
#SerializedName("discount_type")
private String discount_type;
#SerializedName("discount_exp")
private String discount_exp;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDiscount_type() {
return discount_type;
}
public void setDiscount_type(String discount_type) {
this.discount_type = discount_type;
}
public String getDiscount_exp() {
return discount_exp;
}
public void setDiscount_exp(String discount_exp) {
this.discount_exp = discount_exp;
}
}
Any null data happens when the parsing failed.
This is the Java for the response you've shown
public class ProductResponse {
ProductPage products;
}
public class ProductPage {
int current_page;
#SerializedName("data")
private List<Product> products;
}
public class Product {
private int id,price,discount;
private String name,image,description,discount_type,discount_exp;
public Product() { }
}
You have to change like it will work
public class ProductModel {
private int current_page;
private ArrayList<DataModel> data = new ArrayList<>();
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public ArrayList<DataModel> getData() {
return data;
}
public void setData(ArrayList<DataModel> data) {
this.data = data;
}
private class DataModel{
private int id,price,discount,shop_id,vote_id,vedeo_id,status;
private String name,image,description,discount_type,discount_exp,created_at,updated_at;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public int getShop_id() {
return shop_id;
}
public void setShop_id(int shop_id) {
this.shop_id = shop_id;
}
public int getVote_id() {
return vote_id;
}
public void setVote_id(int vote_id) {
this.vote_id = vote_id;
}
public int getVedeo_id() {
return vedeo_id;
}
public void setVedeo_id(int vedeo_id) {
this.vedeo_id = vedeo_id;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDiscount_type() {
return discount_type;
}
public void setDiscount_type(String discount_type) {
this.discount_type = discount_type;
}
public String getDiscount_exp() {
return discount_exp;
}
public void setDiscount_exp(String discount_exp) {
this.discount_exp = discount_exp;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}
}
The issue is that the products are contained within the data field of the JSON response. Retrofit doesn't know this so it can't deserialize the response into a Product array.
You should use it like that
=>Main API Model Class
public class APIResult {
#SerializedName("products")
private ProductModelClass products;
public ProductModelClass getProducts() {
return products;
}
public void setProducts(ProductModelClass products) {
this.products = products;
}
}
=>For data and current page model class
public class ProductModelClass {
#SerializedName("current_page")
private int current_page;
#SerializedName("data")
private ArrayList<ProductDataModel> data;
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public ArrayList<ProductDataModel> getData() {
return data;
}
public void setData(ArrayList<ProductDataModel> data) {
this.data = data;
}
}
=>For Details of products Data
public class ProductDataModel {
#SerializedName("id")
private int id;
#SerializedName("name")
private String name;
#SerializedName("image")
private String image;
#SerializedName("description")
private String description;
#SerializedName("price")
private int price;
#SerializedName("discount")
private int discount;
#SerializedName("shop_id")
private int shop_id;
#SerializedName("discount_type")
private String discount_type;
#SerializedName("discount_exp")
private String discount_exp;
#SerializedName("discount_limit")
private String discount_limit;
#SerializedName("vote_id")
private String vote_id;
#SerializedName("video_id")
private String video_id;
#SerializedName("status")
private String status;
#SerializedName("created_at")
private String created_at;
#SerializedName("updated_at")
private String updated_at;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public int getShop_id() {
return shop_id;
}
public void setShop_id(int shop_id) {
this.shop_id = shop_id;
}
public String getDiscount_type() {
return discount_type;
}
public void setDiscount_type(String discount_type) {
this.discount_type = discount_type;
}
public String getDiscount_exp() {
return discount_exp;
}
public void setDiscount_exp(String discount_exp) {
this.discount_exp = discount_exp;
}
public String getDiscount_limit() {
return discount_limit;
}
public void setDiscount_limit(String discount_limit) {
this.discount_limit = discount_limit;
}
public String getVote_id() {
return vote_id;
}
public void setVote_id(String vote_id) {
this.vote_id = vote_id;
}
public String getVideo_id() {
return video_id;
}
public void setVideo_id(String video_id) {
this.video_id = video_id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}

Error in One to many mapping in hibernate-spring integration

I need to map two pojo class using one to many, but getting the below error
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'testCaseStepsform' at row 1
When I saw the table created by hibernate, I found that a column with data type blob is created
I am adding the code below.
#Entity
#Table(name="TEST_CASE_DESC")
public class TestCaseForm implements Serializable{
/**
*
*/
private static final long serialVersionUID = 10001234L;
#Id
#Column(name="TEST_CASE_ID")
private String testCaseId;
#Column(name="PROJECT_NAME")
private String projectName;
#Column(name="PROJECT_ID")
private String projectId;
#Column(name="RELEASE_NAME")
private String releaseName;
#Column(name="RELEASE_ID")
private String releaseId;
#Column(name="ITERATION")
private String iteration;
#Column(name="TITLE")
private String title;
#Column(name="CREATED_BY")
private String createdBy;
#Column(name="CREATION_DATE")
private Date creationDate;
#Column(name="DESCRIPTION")
private String description;
#Column(name="PRE_CONDITION")
private String preCondition;
#Column(name="POST_CONDITION")
private String postCondition;
#Column(name="TYPE")
private String type;
#Column(name="IMPORTANCE")
private String importance;
private ArrayList<TestCaseStepsForm> testCaseStepsform = new ArrayList<TestCaseStepsForm>();
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPreCondition() {
return preCondition;
}
public void setPreCondition(String preCondition) {
this.preCondition = preCondition;
}
public String getPostCondition() {
return postCondition;
}
public void setPostCondition(String postCondition) {
this.postCondition = postCondition;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getRelease() {
return releaseName;
}
public void setReleaseName(String releaseName) {
this.releaseName = releaseName;
}
public String getReleaseId() {
return releaseId;
}
public void setReleaseId(String releaseId) {
this.releaseId = releaseId;
}
public String getIteration() {
return iteration;
}
public void setIteration(String iteration) {
this.iteration = iteration;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getReleaseName() {
return releaseName;
}
public String getImportance() {
return importance;
}
public void setImportance(String importance) {
this.importance = importance;
}
#OneToMany(mappedBy = "TEST_CASE_DESC", cascade = CascadeType.ALL)
public ArrayList<TestCaseStepsForm> getTestCaseStepsform() {
return testCaseStepsform;
}
public void setTestCaseStepsform(ArrayList<TestCaseStepsForm> testCaseStepsform) {
this.testCaseStepsform = testCaseStepsform;
}
public String getTestCaseId() {
return testCaseId;
}
public void setTestCaseId(String testCaseId) {
this.testCaseId = testCaseId;
}
}
#Entity
#Table(name="TEST_CASE_STEP")
public class TestCaseStepsForm implements Serializable{
/**
*
*/
private static final long serialVersionUID = 123456788091L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="SERIAL_NUMBER")
private String serialNumber;
#Column(name="TEST_CASE_ID")
private String testCaseId;
#Column(name="INPUT")
private String input;
#Column(name="EXPCETED_OUTPUT")
private String expectedOutput;
#Column(name="STATUS")
private String status;
private TestCaseForm testCaseForm;
public String getTestCaseId() {
return testCaseId;
}
public void setTestCaseId(String testCaseId) {
this.testCaseId = testCaseId;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public String getExpectedOutput() {
return expectedOutput;
}
public void setExpectedOutput(String expectedOutput) {
this.expectedOutput = expectedOutput;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
#ManyToOne( fetch = FetchType.LAZY)
#JoinColumn(name = "TEST_CASE_ID", nullable = false)
public TestCaseForm getTestCaseForm() {
return testCaseForm;
}
public void setTestCaseForm(TestCaseForm testCaseForm) {
this.testCaseForm = testCaseForm;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
}
#Configuration
public class TestCaseConfig {
#Bean(name="testCaseForm1")
public TestCaseForm testCaseForm1(){
TestCaseForm tst = new TestCaseForm();
tst.setTestCaseId("1122233");
tst.setProjectId("1234");
tst.setReleaseName("June");
tst.setReleaseId("1707");
tst.setIteration("2");
tst.setProjectName("ExpressPay");
tst.setTitle("ExpressPay");
tst.setCreatedBy("Anirban Deb");
tst.setCreationDate(new Date());
tst.setDescription("ExpressPay Login");
tst.setPreCondition("Active account");
tst.setPostCondition("success");
TestCaseStepsForm str1 = new TestCaseStepsForm();
str1.setTestCaseId("1122233");
str1.setInput("Hello World");
str1.setExpectedOutput("Bye Bye world");
str1.setStatus("Run");
str1.setTestCaseForm(tst);
TestCaseStepsForm str2 = new TestCaseStepsForm();
str2.setTestCaseId("1122233");
str2.setInput("Hello World");
str2.setExpectedOutput("Bye Bye world");
str2.setStatus("Run");
str1.setTestCaseForm(tst);
tst.getTestCaseStepsform().add(str1);
tst.getTestCaseStepsform().add(str2);
return tst;
}
}
public class Main {
public static void main(String[] args) throws MessagingException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("core-bean.xml");
TestCaseForm test1 = context.getBean("testCaseForm1",TestCaseForm.class);
ITestCaseService testCase = context.getBean("testCaseServiceImp", ITestCaseService.class);
testCase.inserTestCase(test1);
[enter image description here][1]
stopWatch.stop();
System.out.println("Time taken in execution : "+stopWatch.getTotalTimeSeconds());
}
}
You should choose to annotate object properties or getters. Don't use it simultaneously.
Apart, #xl0e answer
You need to correct mapping
#OneToMany(mappedBy = "testCaseForm", cascade = CascadeType.ALL)
public ArrayList<TestCaseStepsForm> getTestCaseStepsform() {
return testCaseStepsform;
}

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token. where I am going wrong?

I saw many of stackOverflow answers but I am not getting any clue.
Please answer if you have any suggestion on this.
I have been trying from last evening.
I am trying to parse json response using retrofit.
Json response I am getting :
{"total_entries":3,"skip":0,"limit":100,"items":[{"_id":"583c2281a28f9a9e1a000071","created_at":"2016-11-28T12:26:41Z","last_message":"Hello Dhiren !","last_message_date_sent":1480589762,"last_message_user_id":21053802,"name":"Dhiren Parmar","occupants_ids":[21053802,21054743],"photo":null,"type":3,"updated_at":"2016-12-01T10:56:02Z","user_id":21054743,"xmpp_room_jid":null,"unread_messages_count":0},{"_id":"5841107aa28f9ac86c000031","created_at":"2016-12-02T06:11:06Z","last_message":null,"last_message_date_sent":null,"last_message_user_id":null,"name":"anmol","occupants_ids":[21053802,21155690],"photo":null,"type":3,"updated_at":"2016-12-02T06:11:06Z","user_id":21053802,"xmpp_room_jid":null,"unread_messages_count":0},{"_id":"584110b7a28f9a22eb000008","created_at":"2016-12-02T06:12:07Z","last_message":null,"last_message_date_sent":null,"last_message_user_id":null,"name":"group chat with devi and heet","occupants_ids":[21053802,21062212,21155354],"photo":null,"type":2,"updated_at":"2016-12-02T06:12:07Z","user_id":21053802,"xmpp_room_jid":"49845_584110b7a28f9a22eb000008#muc.chat.quickblox.com","unread_messages_count":0}]}
My response model class :
public class DialogListResponse {
public final long total_entries;
public final long skip;
public final long limit;
public final List<Item> items;
#JsonCreator
public DialogListResponse(#JsonProperty("total_entries") long total_entries,
#JsonProperty("skip") long skip,
#JsonProperty("limit") long limit,
#JsonProperty("items") List<Item> items){
this.total_entries = total_entries;
this.skip = skip;
this.limit = limit;
this.items = items;
}
public static class Item {
#JsonProperty("_id")
public String _id;
#JsonProperty("created_at")
public String created_at;
#JsonProperty("last_message")
public String last_message;
#JsonProperty("last_message_date_sent")
public long last_message_date_sent;
#JsonProperty("last_message_user_id")
public long last_message_user_id;
#JsonProperty("name")
public String name;
#JsonProperty("occupants_ids")
public Set<Integer> occupants_ids;
#JsonProperty("type")
public long type;
#JsonProperty("updated_at")
public String updated_at;
#JsonProperty("user_id")
public long user_id;
#JsonProperty("unread_messages_count")
public long unread_messages_count;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getLast_message() {
return last_message;
}
public void setLast_message(String last_message) {
this.last_message = last_message;
}
public long getLast_message_user_id() {
return last_message_user_id;
}
public void setLast_message_user_id(long last_message_user_id) {
this.last_message_user_id = last_message_user_id;
}
public long getLast_message_date_sent() {
return last_message_date_sent;
}
public void setLast_message_date_sent(long last_message_date_sent) {
this.last_message_date_sent = last_message_date_sent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Integer> getOccupants_ids() {
return occupants_ids;
}
public void setOccupants_ids(Set<Integer> occupants_ids) {
this.occupants_ids = occupants_ids;
}
public long getType() {
return type;
}
public void setType(long type) {
this.type = type;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
public long getUser_id() {
return user_id;
}
public void setUser_id(long user_id) {
this.user_id = user_id;
}
public long getUnread_messages_count() {
return unread_messages_count;
}
public void setUnread_messages_count(long unread_messages_count) {
this.unread_messages_count = unread_messages_count;
}
}}
Retorfit portion that is responsible for pasring :
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(JacksonConverterFactory.create(Utility.getJsonMapper()));
public static <S> S createService(Class<S> serviceClass) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor);
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
Logcat error :

JsonArray with different jsonObjects To ArrayList android using gson [duplicate]

This question already has an answer here:
How to convert JSONArray to List with Gson?
(1 answer)
Closed 6 years ago.
How to convert a jsonArray with multiple jsonObjects to an arraylist using Gson.
Here is my Json String:
[{"AccommoAddress":{
"PostalCode":2109,
"Street":"22 Ararat Str",
"Town":"Westdene"
},
"AccommoDetails":{
"AccommoId":0,
"CleaningService":1,
"DSTV":1,
"DedicatedStudyArea":1
},
"AccommoID":1,
"AccommoMainImage":{
"CategoryId":0,
"ContentType":".png",
"DateUploaded":"2016-07-16",
"FileSize":2362,
"ImageCategory":"MAIN",
"ImageId":1,
"ImageName":"images.png"
}]
First , it's not a correct Json format.
"ImageName":"images.png" <- (you need to put a "}" here )
Second , create a entity
public class TargetEntity {
private AccommoAddressEntity AccommoAddress;
private AccommoDetailsEntity AccommoDetails;
private int AccommoID;
private AccommoMainImageEntity AccommoMainImage;
public AccommoAddressEntity getAccommoAddress() {
return AccommoAddress;
}
public void setAccommoAddress(AccommoAddressEntity AccommoAddress) {
this.AccommoAddress = AccommoAddress;
}
public AccommoDetailsEntity getAccommoDetails() {
return AccommoDetails;
}
public void setAccommoDetails(AccommoDetailsEntity AccommoDetails) {
this.AccommoDetails = AccommoDetails;
}
public int getAccommoID() {
return AccommoID;
}
public void setAccommoID(int AccommoID) {
this.AccommoID = AccommoID;
}
public AccommoMainImageEntity getAccommoMainImage() {
return AccommoMainImage;
}
public void setAccommoMainImage(AccommoMainImageEntity AccommoMainImage) {
this.AccommoMainImage = AccommoMainImage;
}
public static class AccommoAddressEntity {
private int PostalCode;
private String Street;
private String Town;
public int getPostalCode() {
return PostalCode;
}
public void setPostalCode(int PostalCode) {
this.PostalCode = PostalCode;
}
public String getStreet() {
return Street;
}
public void setStreet(String Street) {
this.Street = Street;
}
public String getTown() {
return Town;
}
public void setTown(String Town) {
this.Town = Town;
}
}
public static class AccommoDetailsEntity {
private int AccommoId;
private int CleaningService;
private int DSTV;
private int DedicatedStudyArea;
public int getAccommoId() {
return AccommoId;
}
public void setAccommoId(int AccommoId) {
this.AccommoId = AccommoId;
}
public int getCleaningService() {
return CleaningService;
}
public void setCleaningService(int CleaningService) {
this.CleaningService = CleaningService;
}
public int getDSTV() {
return DSTV;
}
public void setDSTV(int DSTV) {
this.DSTV = DSTV;
}
public int getDedicatedStudyArea() {
return DedicatedStudyArea;
}
public void setDedicatedStudyArea(int DedicatedStudyArea) {
this.DedicatedStudyArea = DedicatedStudyArea;
}
}
public static class AccommoMainImageEntity {
private int CategoryId;
private String ContentType;
private String DateUploaded;
private int FileSize;
private String ImageCategory;
private int ImageId;
private String ImageName;
public int getCategoryId() {
return CategoryId;
}
public void setCategoryId(int CategoryId) {
this.CategoryId = CategoryId;
}
public String getContentType() {
return ContentType;
}
public void setContentType(String ContentType) {
this.ContentType = ContentType;
}
public String getDateUploaded() {
return DateUploaded;
}
public void setDateUploaded(String DateUploaded) {
this.DateUploaded = DateUploaded;
}
public int getFileSize() {
return FileSize;
}
public void setFileSize(int FileSize) {
this.FileSize = FileSize;
}
public String getImageCategory() {
return ImageCategory;
}
public void setImageCategory(String ImageCategory) {
this.ImageCategory = ImageCategory;
}
public int getImageId() {
return ImageId;
}
public void setImageId(int ImageId) {
this.ImageId = ImageId;
}
public String getImageName() {
return ImageName;
}
public void setImageName(String ImageName) {
this.ImageName = ImageName;
}
}
}
Third
Gson gson = new Gson();
TargetEntity[] target = gson.fromJson(JsonString,TargetEntity[].class)

Categories