firebase到底怎么用android

1、后台注册

https://console.firebase.google.com/project/

在firebase后台创建一个android应用,拿到google-services.json

2、配置数据库

在firebase后台Realtime Database中找到Data添加数据,这里直接导入一个json文件就好。举例子,自己写一个recipes-db.json,然后点击import json就可以将数据保存到firebase后台。

recipes-db.json文件要json格式的,实例如下

{
  "recipes" : {
    "recipe1" : {
      "calories" : 331,
      "category" : 1,
      "cook_time" : 10,
      "description" : "A low-fat and very tasty option for summer grilling/broiling.",
      "directions" : [ null, "Preheat an outdoor grill for medium-high heat and lightly oil the grate.", "Combine turkey, bread crumbs, spinach, Greek dressing, feta cheese, onion, egg, salt, and black pepper together in a bowl; form into patties.", "Cook turkey burgers on preheated grill until no longer pink in the center and the juices run clear, 5 to 10 minutes per side. An instant-read thermometer inserted into the center should read at least 165 degrees F (74 degrees C)." ],
      "footnote" : "I found that chopping the spinach and the onion (separately) in a food processor gave the burgers a smooth consistency.",
      "image" : "https://images.media-allrecipes.com/userphotos/560x315/7748003.jpg",
      "ingredients" : [ null, "2 pounds ground turkey", "1 1/2 cups fresh bread crumbs", "1 1/2 cups chopped baby spinach", "1/2 cup light Greek dressing", "5 ounces feta cheese, cubed", "1/4 large onion, finely chopped", "1 egg", "salt and ground black pepper to taste" ],
      "name" : "Greek Turkey Burgers",
      "prep_time" : 15,
      "serving" : 8
    },
    "recipe2" : {
      "calories" : 354,
      "category" : 1,
      "cook_time" : 10,
      "description" : "This is an awesome turkey burger with garlic, pesto, feta cheese, and seasoned salt. Even my husband liked it and he typically will only eat traditional burgers!",
      "directions" : [ null, "Preheat an outdoor grill for medium-high heat.", "Mix together ground turkey, pesto, garlic, feta cheese, 1 1/2 teaspoons seasoned salt, and breadcrumbs in a bowl until evenly blended. Form into 4 patties.", "Grill pesto burgers until no longer pink in the center, about 5 minutes per side. Sprinkle with seasoned salt halfway through cooking." ],
      "footnote" : "Reynolds® Aluminum foil can be used to keep food moist, cook it evenly, and make clean-up easier.",
      "image" : "https://images.media-allrecipes.com/userphotos/560x315/1117627.jpg",
      "ingredients" : [ null, "1 1/4 pounds lean ground turkey", "2 tablespoons basil pesto", "1 teaspoon minced garlic", "1/2 cup crumbled feta cheese", "1 1/2 teaspoons seasoned salt", "1/2 cup bread crumbs", "1/2 teaspoon seasoned salt" ],
      "name" : "Pesto Turkey Burgers",
      "prep_time" : 10,
      "serving" : 4
    }
  }
}

点击rule设置访问规则,举例子

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

3、android工程添加依赖

project.gradle中

repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:4.1.3'
    classpath 'com.google.gms:google-services:4.3.5'
}

app.gradle中

apply plugin: 'com.google.gms.google-services'
dependencies {
    implementation platform('com.google.firebase:firebase-bom:27.1.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-database:19.7.0'
}

4、代码中取数据

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
public class SvtpCamera extends Application {
    @Override
    public void onCreate() {
        super.onCreate();        
        DatabaseReference database = FirebaseDatabase.getInstance().getReference();
        DatabaseReference ref = database.child("recipes");
        System.err.println("DatabaseReference ref "+ ref);
        Query phoneQuery = ref;
        phoneQuery.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                System.err.println("onDataChange dataSnapshot "+ dataSnapshot);              
                    for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                        System.err.println("onDataChange "+singleSnapshot.toString());
                        Long category = (Long) singleSnapshot.child("calories").getValue();
                    }  
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.err.println("onCancelled"+ databaseError.toException());
            }
        });
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值