I am trying to build POJO for my firebase RealTime Database. Am i doing it correctly according to my Realtime Database? Link below
detailData,detailContent,detailTitleContent,isDetail,titleContent they named the same everywhere,they just have different text in them.
public class POJO {
private String titleContent;
private String detailContent;
private String detailTitleContent;
private List<String> detailData = new ArrayList<>();
private List<String> textInfo = new ArrayList<>();
private boolean isDetail;
private boolean isList;
public POJO() {
}
public POJO(String titleContent, String detailContent, String
detailTitleContent, List<String> detailData, List<String> textInfo,
boolean isDetail, boolean isList) {
this.titleContent = titleContent;
this.detailContent = detailContent;
this.detailTitleContent = detailTitleContent;
this.detailData = detailData;
this.textInfo = textInfo;
this.isDetail = isDetail;
this.isList = isList;
}
public String getTitleContent() {
return titleContent;
}
public String getDetailContent() {
return detailContent;
}
public String getDetailTitleContent() {
return detailTitleContent;
}
public List<String> getDetailData() {
return detailData;
}
public List<String> getTextInfo() {
return textInfo;
}
public boolean isDetail() {
return isDetail;
}
public boolean isList() {
return isList;
}
}
Based on the following response (which you've provided), I'll be creating the POJO Classes
{
"datas": [{
"detailData": [{
"detailContent": "<p>LOTS of information</p>",
"detailTitleContent": "Title"
}, {
"detailContent": "<p>Lots of more information!</p>",
"detailTitleContent": "Second Title"
}],
"isDetail": false,
"titleContent": "Last Title"
}]
}
Therefore, looking at this response, you can see that your first (Let's name is "MyPojo") class will have an array of "datas" object.
public class MyPojo
{
private Datas[] datas;
public Datas[] getDatas (){
return datas;
}
public void setDatas (Datas[] datas){
this.datas = datas;
}
}
Now we have to make a model object for the "Datas":
public class Datas
{
private String isDetail;
private String titleContent;
private DetailData[] detailData;
public String getIsDetail (){
return isDetail;
}
public void setIsDetail (String isDetail){
this.isDetail = isDetail;
}
public String getTitleContent (){
return titleContent;
}
public void setTitleContent (String titleContent){
this.titleContent = titleContent;
}
public DetailData[] getDetailData (){
return detailData;
}
public void setDetailData (DetailData[] detailData){
this.detailData = detailData;
}
}
Last but not least, "DetailData" model which is another array:
public class DetailData
{
private String detailTitleContent;
private String detailContent;
public String getDetailTitleContent (){
return detailTitleContent;
}
public void setDetailTitleContent (String detailTitleContent){
this.detailTitleContent = detailTitleContent;
}
public String getDetailContent (){
return detailContent;
}
public void setDetailContent (String detailContent){
this.detailContent = detailContent;
}
}
From here, you should have a complete Pojo for your JSON response and ready to be handled. Just want to point 2 things out for your benefit:
1. I highly recommend you reading the following tutorial Android JSON Parsing Tutorial and pay close attention to the The difference between [ and { – (Square brackets and Curly brackets) section as you want to gain in-depth understanding of JSONArray and JSONObject.
2. Use JSONLint to validate your JSON response as it's helpful sometimes and also use Convert XML or JSON to Java Pojo Classes - Online tool to generate the Pojo classes based on the JSON response (I used it myself in this case). The major benefit behind this is accuracy, takes less than 1 minute to copy and implement.
Good luck and let me know if you need further assistance :)