How to parse this xml using simple xml - android

<folder
id="-1"
name="BaseFolder" >
<folder
id="1047"
name="Folder1" >
<folder
id="1050"
name="Folder1.1" />
</folder>
<folder
id="1048"
name="Folder2" >
<folder
id="1053"
name="Folder2.1" />
<folder
id="1054"
name="Folder2.2" />
</folder>
<folder
id="1049"
name="Folder3" >
<folder
id="1051"
name="Folder3.1" />
<folder
id="1052"
name="Folder3.2" />
</folder>
</folder>
These are the classes im using
#Root
public class Response extends BaseResponse {
private static final long serialVersionUID = 1L;
#ElementList(entry="folder", inline=true)
public ArrayList<Folder> folderList;
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
#Attribute
private String success;
}
Folder class
#Root
public class Folder extends BaseResponse {
private static final long serialVersionUID = 1L;
#Attribute
private String id;
#Attribute
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
How can i access the inner folder tags?
In the activity im calling
List <Folder>folders=new ArrayList<Folder>();
Response response = new Response();
response = directoryService.getResponses();
folders= response.folderList;
for(int i=0;i<folders.size();i++)
{
Log.e("Contents of folders are ",folders.get(i).getName());
}
Im able to print BaseFolder.But how to access the inner tags and how can i get them in a list so that i can show them in some list view.Does any one have any idea?

I changed the Folder object to
#Root
public class Folder extends BaseResponse {
private static final long serialVersionUID = 1L;
#ElementList(entry="folder", inline=true,name="name",required=false)
public ArrayList<Folder> llfolder;
public ArrayList<Folder> getLlfolder() {
return llfolder;
}
public void setLlfolder(ArrayList<Folder> llfolder) {
this.llfolder = llfolder;
}
#Attribute(required=false)
private String id;
#Attribute(required=false)
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Related

Java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY when want to get data into recyclerview

I have a json. I have to get source_url into my recyclerView -
[{
"id":3110,
"date":"2020-05-07T18:33:44",
"date_gmt":"2020-05-07T18:33:44",
"modified":"2020-05-07T18:35:37",
"modified_gmt":"2020-05-07T18:35:37",
"_embedded":{
"wp:featuredmedia":[
{
"id":3111,
"date":"2020-05-07T18:33:08",
"slug":"prof-dr-abul-khair",
"source_url":"https:\/\/www.healthmen.com.bd\/wpcontent\/uploads\/2020\/05\/Prof.-Dr.-Abul-Khair-scaled.jpg"
}]
}
}]
I want to get source_url from this JSON
But, here is an array wp:featuredmedia. So that, I followed this process-
Created a class named FeaturedMedia -
public class FeaturedMedia {
#SerializedName("source_url")
#Expose
private String souceurl;
public String getSourceurl(){
return sourceurl;
}
}
Then, I created another class named MediaDetails where I take the FeaturedMedia as a List-
public class MediaDetails{
#SerializedName("wp:featuredmedia")
List<FeaturedMedia> featureMediaList;
public List<FeaturedMedia> getFeaturedMediaList(){
return featuredMediaList;
}}
Then the Model Class-
public class Model{
#SerializedName("id")
#Expose
int id;
#SerializedName("_embedded")
#Expose
MediaDetalis embedded;
public int getId(){
return id;
}
public MediaDetails getEmbedded(){
return embedded;
}}
After all, I created RecyclerViewAdapter to get the data-
public CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomAdapterHolder{
List<Model> modelList;
public void onBindViewHolder(#NonNull CustomAdapterHolder holder, int position){
final Model model=modelList.get(position);
int id= model.getId():
String embedded= String.valurOf(model.getEmbedded.getFeaturedMediaList());
}
But, embedded can not get the source_url value. I completed this CustomAdapter. Here I just presented needed code of that adapter. How can I get the source_url value in this RecyclerView?
Your POJO class will be (Respect to given JSON)
public class Model {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("date")
#Expose
private String date;
#SerializedName("date_gmt")
#Expose
private String dateGmt;
#SerializedName("modified")
#Expose
private String modified;
#SerializedName("modified_gmt")
#Expose
private String modifiedGmt;
#SerializedName("_embedded")
#Expose
private Embedded embedded;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDateGmt() {
return dateGmt;
}
public void setDateGmt(String dateGmt) {
this.dateGmt = dateGmt;
}
public String getModified() {
return modified;
}
public void setModified(String modified) {
this.modified = modified;
}
public String getModifiedGmt() {
return modifiedGmt;
}
public void setModifiedGmt(String modifiedGmt) {
this.modifiedGmt = modifiedGmt;
}
public Embedded getEmbedded() {
return embedded;
}
public void setEmbedded(Embedded embedded) {
this.embedded = embedded;
}
}
Then Embedded class
public class Embedded {
#SerializedName("wp:featuredmedia")
#Expose
private List<WpFeaturedmedium> wpFeaturedmedia = null;
public List<WpFeaturedmedium> getWpFeaturedmedia() {
return wpFeaturedmedia;
}
public void setWpFeaturedmedia(List<WpFeaturedmedium> wpFeaturedmedia) {
this.wpFeaturedmedia = wpFeaturedmedia;
}
}
Then WpFeaturedmedium
public class WpFeaturedmedium {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("date")
#Expose
private String date;
#SerializedName("slug")
#Expose
private String slug;
#SerializedName("source_url")
#Expose
private String sourceUrl;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getSourceUrl() {
return sourceUrl;
}
public void setSourceUrl(String sourceUrl) {
this.sourceUrl = sourceUrl;
}
}
Check - How to create POJO class

Model structure to parse JSON with Retrofit [duplicate]

This question already has answers here:
Why does Gson fromJson throw a JsonSyntaxException: Expected BEGIN_OBJECT but was BEGIN_ARRAY?
(2 answers)
GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?
(11 answers)
Closed 4 years ago.
I have the following JSON structure:
[
{
"id":1,
"name":"car",
"elements":[
{
"id":1,
"name":"price",
"type":"textField",
"constraints":"blablabla"
},
{
"id":2,
"name":"color",
"type":"textField",
"constraints":"blablabla"
},
{
"id":3,
"name":"images",
"type":"image",
"constraints":"blablabla"
}
]
}
]
And I have the following models:
public class Product {
private Long id;
private String name;
#Expose
private Element elements;
public Product(Long id, String name, Element elements) {
this.id = id;
this.name = name;
this.elements = elements;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Element getElements() {
return elements;
}
}
public class Element {
private Long id;
private String name;
private String type;
private String constraints;
public Element(Long id, String name, String constraints, String type) {
this.id = id;
this.type=type;
this.name = name;
this.constraints = constraints;
}
public String getType() {
return type;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getConstraints() {
return constraints;
}
}
The Elements model is which I have problems, I am getting the following error:
ERROR: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 35 path $[0].elements
How can I change the model to make it work? I tried to change the elements in Product class to JSONObject array but when I wanted to parse, it was empty.
You need to modify your POJOs like this:
Element.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Element {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("type")
#Expose
private String type;
#SerializedName("constraints")
#Expose
private String constraints;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getConstraints() {
return constraints;
}
public void setConstraints(String constraints) {
this.constraints = constraints;
}
}
Product.java
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Product {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("elements")
#Expose
private List<Element> elements = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Element> getElements() {
return elements;
}
public void setElements(List<Element> elements) {
this.elements = elements;
}
}
P.S: You should use 'http://www.jsonschema2pojo.org/' website for generating POJO automatically from the JSON string you provide to it.
I hope this helps.
I think you try to put an array of Elements into "private Element elements" which is a single object
Change your pojo with this. Also you can add your respective constructor which you require.
public class Element {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("type")
#Expose
private String type;
#SerializedName("constraints")
#Expose
private String constraints;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getConstraints() {
return constraints;
}
public void setConstraints(String constraints) {
this.constraints = constraints;
}
}
public class Product {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("elements")
#Expose
private List<Element> elements = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Element> getElements() {
return elements;
}
public void setElements(List<Element> elements) {
this.elements = elements;
}
}
in json you have list of Element and in the model you declared Element as an Object
change this private Element elements; to List of Element
Try this pojo or you can make from this link
private String id;
private String name;
private ArrayList<Elements> elements;
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public ArrayList<Elements> getElements ()
{
return elements;
}
public void setElements (ArrayList<Elements> elements)
{
this.elements = elements;
}
#Override
public String toString()
{
return "ClassPojo [id = "+id+", name = "+name+", elements = "+elements+"]";
}
public class Elements
{
private String id;
private String name;
private String constraints;
private String type;
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public String getConstraints ()
{
return constraints;
}
public void setConstraints (String constraints)
{
this.constraints = constraints;
}
public String getType ()
{
return type;
}
public void setType (String type)
{
this.type = type;
}
#Override
public String toString()
{
return "ClassPojo [id = "+id+", name = "+name+", constraints = "+constraints+",
type = "+type+"]";
}
}

How I can map this three class into Realm?

I have 3 (POJO) classes which I have requested from NewsApi by Retrofit. I should take this class and make a RealmObject. How can I do it?
Here this classes :
public class Source {
#SerializedName("id") #Expose private Object id;
#SerializedName("name") #Expose private String name;
public Object getId() { return id;}
public void setId(Object id) { this.id = id;}
public String getName() { return name;}
public void setName(String name) { this.name = name;}
}
Second class :
public class Article {
#SerializedName("source") #Expose private Source source;
#SerializedName("author") #Expose private String author;
#SerializedName("title") #Expose private String title;
#SerializedName("description") #Expose private String description;
#SerializedName("url") #Expose private String url;
#SerializedName("urlToImage") #Expose private String urlToImage;
#SerializedName("publishedAt") #Expose private String publishedAt;
public Source getSource() {return source;}
public void setSource(Source source) { this.source = source;}
public String getAuthor() { return author;}
public void setAuthor(String author) { this.author = author;}
public String getTitle() {return title;}
public void setTitle(String title) {this.title = title;}
public String getDescription() {return description;}
public void setDescription(String description) {
this.description = description;}
public String getUrl() { return url;}
public void setUrl(String url){this.url = url;}
public String getUrlToImage() {return urlToImage;}
public void setUrlToImage(String urlToImage) {
this.urlToImage = urlToImage;
}
public String getPublishedAt() {
return publishedAt;}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
}
Third class
public class ArticlesResponse {
#SerializedName("status") private String status;
#SerializedName("sortBy") private String sortBy;
#SerializedName("source") private String source;
#SerializedName("articles") private List<Article> articles;
public ArticlesResponse() {}
public String getStatus() {return status;}
public void setStatus(String status) { this.status = status;}
public String getSortBy() { return sortBy;}
public void setSortBy(String sortBy) {this.sortBy = sortBy;}
public String getSource() { return source;}
public void setSource(String source) { this.source = source;}
public List<Article> getArticles() { return articles;}
public void setArticles(List<Article> articles) {this.articles = articles;}
public ArticlesResponse getArticleResponse() { return null;}
}
I thought that I should make three classes which would extentnds RealmObject.
class Source : extends RealmObject {}
class Article : extends RealmObject {and here RealmList<Source> }
class ArticleResponse : extends RealmObject {and here RealmList<Article>}
but what about fields which are Objects?
private Object id;

Professional way to handle model classes when using GSON and SQLight Android app

In My application I get data from a web service and display those data in recycler view. After that I'm planing to add those data in to local sqlite database and display those data when user open application without internet connection.
Here's a simple model class I'm using to pars JSON result using GSON
public class Repo implements Parcelable {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("url")
#Expose
private String url;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id);
dest.writeString(this.name);
dest.writeString(this.url);
}
public Repo() {
}
protected Repo(Parcel in) {
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
this.name = in.readString();
this.url = in.readString();
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public static final Creator<Repo> CREATOR = new Creator<Repo>() {
#Override
public Repo createFromParcel(Parcel source) {
return new Repo(source);
}
#Override
public Repo[] newArray(int size) {
return new Repo[size];
}
};
}
I can create a almost identical model class to represent SQLite data. In here I'm using ORMlite. But this is very similar situation for other ORMs.
#DatabaseTable(tableName = Repo.TABLE_NAME)
public class Repo {
public static final String TABLE_NAME = "repo";
#DatabaseField(columnName = "repo_id")
private long repoId;
#DatabaseField(columnName = "name")
private String name;
public long getRepoId() {
return repoId;
}
public String getName() {
return name;
}
public void setRepoId(long repoId) {
this.repoId = repoId;
}
public void setName(String name) {
this.name = name;
}
}
But by the time I'm trying to save these data in to SQLite database I already have data objects set in GSON model classes. It's kind a redundant thing copy object from GSON model and setting that values in to SQLite model. So I came up with below solution by trying to use single model class to represent both.
#DatabaseTable(tableName = Repo.TABLE_NAME)
public class Repo implements Parcelable {
public static final String TABLE_NAME = "repo";
#DatabaseField(columnName = "repo_id")
#SerializedName("id")
#Expose
private Integer id;
#DatabaseField(columnName = "name")
#SerializedName("name")
#Expose
private String name;
#SerializedName("url")
#Expose
private String url;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id);
dest.writeString(this.name);
dest.writeString(this.url);
}
public Repo() {
}
protected Repo(Parcel in) {
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
this.name = in.readString();
this.url = in.readString();
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public static final Creator<Repo> CREATOR = new Creator<Repo>() {
#Override
public Repo createFromParcel(Parcel source) {
return new Repo(source);
}
#Override
public Repo[] newArray(int size) {
return new Repo[size];
}
};
}
I have try this with different type of model class where it only had String type fields. Since GSON uses types like Integer,Boolean That stopping me from using same model for SQLite because database does not identify Integer as a type, in order to work it need to be int.
So what is the professional way to handle this ? Don't I have any other option other than going back to the method of creating two separate model class to represent SQLite and GSON.
Yout approach is absolutely correct, but i think you are putting too much effort reinventing the wheel
You can easily achieve the described task using Room

How to be with identical in content classes using by Realm

I need to create many identical in content classes like the class below
public abstract class AbstractListModel extends RealmObject {
#PrimaryKey
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
If I will extend this class in another, it seems I'll have a lot of empty classes, 'cause they have only 2 fields (id and name) wich contains in the mother-class.
public class LectureHallListModel extends AbstractListModel {
//#PrimaryKey
//private String id;
//private String name;
//public String getId() {
// return id;
//}
//public void setId(String id) {
// this.id = id;
//}
//public String getName() {
// return name;
//}
//public void setName(String name) {
// this.name = name;
//}
}
Are the any way to add to a DB several identical in content tables without creation empty classes?Thank you!
Inheritance of fields (technically from any class that is not directly RealmObject) is not supported by Realm.
You would need the following setup:
public interface AbstractListModel {
String getId();
void setId(String id);
String getName();
void setName(String name);
}
And
public class LectureHallListModel extends RealmObject implements AbstractListModel {
#PrimaryKey
private String id;
private String name;
#Override
public String getId() {
return id;
}
#Override
public void setId(String id) {
this.id = id;
}
#Override
public String getName() {
return name;
}
#Override
public void setName(String name) {
this.name = name;
}
}

Categories

Resources