我使用volley来解析来自newsAPI.org的新闻数据。我想将响应保存到Firebase以供离线查看和持久化。
这是来自API的示例回复:
articles: [
{
author: "Megan Rose Dickey",
title: "Ojo wants to be the electric scooter for commuters, but...",
description: "Commuting in a busy city like San Francisco can be
annoying..",
url: "https://techcrunch.com/2017/08/23/ojo-wants-to-be-the-electric-
scooter-for-commuters-but-its-not-there-yet/",
urlToImage: "https://img.vidible.tv/prod/2017",
publishedAt: "2017-08-23T21:19:56Z"
},
{
author: "Katie Roof",
title: "Pishevar intervenes in Benchmark-Kalanick lawsuit",
description: "Early Uber investor and former board member Shervin
Pishevar is speaking out against Benchmark again..",
url: "https://techcrunch.com/2017/08/24/pishevar-sends-another-letter-
to-uber-board-about-benchmark/",
urlToImage:"https://tctechcrunch2011.files.wordpress.com/",
publishedAt: "2017-08-24T22:49:59Z"
},
总共我在articles数组中有5个对象。 我想将每个对象存储在Firebase数据库中。这就是我的尝试:
StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.NEWS_ENDPOINT,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response != null){
Log.d(TAG, "News Api Response is: \t" + response.toString());
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray articles = jsonObject.getJSONArray("articles");
for (int i = 0; i < articles.length(); i++){
JSONObject items = articles.getJSONObject(i);
final String title_news = items.getString("title");
final String desc_news = items.getString("description");
final String urlImg = items.getString("urlToImage");
final String author_news = items.getString("author");
final String url = items.getString("url");
final String publishedAt = items.getString("publishedAt");
NewsItem newsItem = new NewsItem(author_news, title_news, desc_news, url, urlImg, publishedAt);
itemList.add(newsItem);
/**
* Save JSON Results to Firebase
* */
for (int k = 0; k < articles.length(); k++){
HashMap hashMap = new HashMap();
hashMap.put("newsTitle", title_news);
hashMap.put("newsDesc", desc_news);
hashMap.put("newsImageUrl", urlImg);
hashMap.put("newsAuthor", author_news);
hashMap.put("newsUrl", url);
hashMap.put("newsDate", publishedAt);
newsRootRef.setValue(hashMap);
}
我希望将所有对象AS-IS存储在响应数组中,然后再检索它们。还有另一种方法吗?谢谢,抱歉这篇长篇文章。
答案 0 :(得分:0)
在这种情况下,您需要使用push()来存储数据。否则,您只是在每次迭代时替换引用处的数据。这就是为什么似乎只存储最后一条记录的原因。尝试更改此行:
newsRootRef.setValue(hashMap);
......进入这个:
newsRootRef.push().setValue(hashMap);
为避免重复条目,我建议您从Firebase中获取所有条目,并在HashSet中缓存url属性(因为此属性似乎是唯一的)。然后你可以修改你的代码:
if (!urlSet.contains(url)) {
HashMap hashMap = new HashMap();
hashMap.put("newsTitle", title_news);
hashMap.put("newsDesc", desc_news);
hashMap.put("newsImageUrl", urlImg);
hashMap.put("newsAuthor", author_news);
hashMap.put("newsUrl", url);
hashMap.put("newsDate", publishedAt);
newsRootRef.push(),setValue(hashMap);
}
但是当然你需要首先填充你的HashSet所以我建议做这样的事情:
final Set<String> urlSet = new HashSet<>();
newsRootRef.addChildEventListener(new ChildEventListener() {
int i = 0;
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
urlSet.add(dataSnapshot.getValue(String.class));
if (i++ == dataSnapshot.getChildrenCount()) {
...
...your code...
StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.NEWS_ENDPOINT,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
...
...
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});