【问题标题】:how to fetch data from firebase realtime database如何从firebase实时数据库中获取数据
【发布时间】:2019-06-13 10:40:11
【问题描述】:

我正在尝试从 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: 不支持转换为数组,请改用 List

我想在单个 arrayList 中获取所有国家及其电台数据,但由于这个异常,我无法做到,请帮助我。提前谢谢你

【问题讨论】:

  • 这就是你得到的确切例外?请将其添加到您的问题中。

标签: java android firebase-realtime-database


【解决方案1】:

station Array更改为Response.java中的List,如下所示。

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()+"]";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多