繁体   English   中英

如何从firebase实时数据库中获取数据

[英]how to fetch data from firebase realtime database

我正在尝试从firebase实时数据库中获取数据到我的应用程序时使用pojo类转换数据对象即时获取异常请帮助我

这是json格式的数据,我试图从firebase获取它是json数组,其中包含国家名称和各自国家的无线电台aaray

[ {
                  "country" : "india",
                  "station" : [ {
                    "image" : "https://firebasestorage.googleapis.com/v0/b/testing-radio-app.appspot.com/o/RadioUi%2F1.jpg?alt=media&token=1ff817e1-7caa-4074-927e-dfca0f7b49aa",
                    "title" : "India1",
                    "url" : "http://prclive1.listenon.in:9960"
                  }, {
                    "image" : "https://firebasestorage.googleapis.com/v0/b/testing-radio-app.appspot.com/o/RadioUi%2F2.jpg?alt=media&token=cb03a047-f51c-478d-8b0c-26379adba9a1",
                    "title" : "India2",
                    "url" : "http://192.240.102.133:11454/stream"
                  } ]
                }, {
                  "country" : "us",
                  "station" : [ {
                    "image" : "https://firebasestorage.googleapis.com/v0/b/testing-radio-app.appspot.com/o/RadioUi%2Fmango.png?alt=media&token=ee11dcab-7ce4-4057-b045-2107b07bc97a",
                    "title" : "US1",
                    "url" : "http://7619.live.streamtheworld.com:3690/977_HITS_SC"
                  } ]
                } ]



                 **// here is my java code in side mainActivity here is firebase code**




  DatabaseReference database = 
 FirebaseDatabase.getInstance().getReference();
                            database.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            **//here im getting all country array**
               Iterable<DataSnapshot> contact = dataSnapshot.getChildren();
                       for (DataSnapshot d:contact)
                                    {

            **//here im trying to read array data one by one and type cast it with pojo class but on this line im getting run time exception**
            Response res= d.getValue(Response.class);
                                        Log.d("data", "onDataChange: "+res.toString());

                                    }



                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError databaseError) {

                                }
                            });

                **these are my two pojo classes

                1.Station.java class contains radio station name, image, and url**

                    public class Station
                    {
                        private String image;

                        private String title;

                        private String url;

                        public String getImage ()
                        {
                            return image;
                        }

                        public void setImage (String image)
                        {
                            this.image = image;
                        }

                        public String getTitle ()
                        {
                            return title;
                        }

                        public void setTitle (String title)
                        {
                            this.title = title;
                        }

                        public String getUrl ()
                        {
                            return url;
                        }

                        public void setUrl (String url)
                        {
                            this.url = url;
                        }

                        @Override
                        public String toString()
                        {
                            return "ClassPojo [image = "+image+", title = "+title+", url = "+url+"]";
                        }

                **2.Response.java class which contains conutry name and station array**

                    public class Response
                    {
                        private String country;

                        private Station[] station;

                        public String getCountry ()
                        {
                            return country;
                        }

                        public void setCountry (String country)
                        {
                            this.country = country;
                        }

                        public Station[] getStation ()
                        {
                            return station;
                        }

                        public void setStation (Station[] station)
                        {
                            this.station = station;
                        }

                        @Override
                        public String toString()
                        {
                            return "ClassPojo [country = "+country+", station = "+station+"]";
                        }
                    }

我正在考虑这个例外

com.google.firebase.database.DatabaseException:不支持转换为数组,请使用Listsinstead

我想在单个arrayList中获取所有国家及其站点数据,但由于这个例外,我无法做到,请帮助我。 先感谢您

站点 阵列更改为Response.java中的列表 ,如下所示。

public class Response
{
    private String country;

    private List<Station> station;

    public String getCountry ()
    {
        return country;
    }

    public void setCountry (String country)
    {
        this.country = country;
    }

    public List<Station> getStation ()
    {
        return station;
    }

    public void setStation (List<station> station)
    {
        this.station = station;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [country = "+country+", station = "+station.toString()+"]";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM