当我尝试将带有pojo类的数据对象转换为异常时,我试图从Firebase实时数据库中获取数据到我的应用程序中
这里是json格式的数据,我试图从firebase中获取它是json数组,其中包含国家/地区名称和相应国家/地区的广播电台
[ {
"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:不支持转换为数组,请使用Listsin代替
我想在单个arrayList中获取所有国家及其站点数据,但是由于此异常,我无法执行此操作,请帮帮我。预先谢谢你
答案 0 :(得分:0)
在Response.java中将 station Array 更改为 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()+"]";
}
}