gpt4 book ai didi

java - 如何从firebase实时数据库获取数据

转载 作者:行者123 更新时间:2023-12-02 04:31:49 25 4
gpt4 key购买 nike

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

这是 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:Converting to Arrays is not supported, please use Listsinstead

我想在单个 arrayList 中获取所有国家/地区及其电台数据,但由于此异常,我无法执行此操作,请帮助我。预先感谢您

最佳答案

将 Response.java 中的station数组更改为列表,如下所示。

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

关于java - 如何从firebase实时数据库获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56578678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com