2017-08-30 103 views
1

好吧,Android - Firebase字符串返回'null'

我觉得这很奇怪。 我有一个开放的Firebase数据库的Android Studio项目(读/写:“true”),并且在那里我设法写入和检索数据。

现在我开始了一个新项目,与我的Android Studio同步并下载了'google-service.json'。和以前一样。

现在我的Android项目能够写入数据,但无法检索数据。奇怪! 我搜索了网页,主要是重定向到StackOverflow并尝试所有。这完全不难,但我不知道哪里出了问题。

  • 我gradle产出的有:

    repositories { 
        maven { 
         url "https://maven.google.com" 
        } 
    } 
    

编译 'com.google.firebase:火力数据库:11.0.4'

  • 我的项目是同步化:我已按照步骤“工具”>“Firebase”>“实时数据库”进行操作。
  • Android Studio使用正确的帐户登录;
  • 我已经下载了'google-service.json'并将其放在正确的位置;
  • 我的火力地堡的格式如下:

CURRENTVERSION: “2.06”

没什么特别you'ld认为。

  • 得到这个代码:

    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(); 
    DatabaseReference databaseReference = firebaseDatabase.getReference("currentVersion"); 
    
    databaseReference.addValueEventListener(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         // This method is called once with the initial value and again 
         // whenever data at this location is updated. 
         versionCheckFirebase = dataSnapshot.getValue(String.class); 
        } 
    
        @Override 
        public void onCancelled(DatabaseError error) { 
         // Failed to read value 
        } 
    }); 
    
    Toast.makeText(this, versionCheckFirebase, Toast.LENGTH_SHORT).show(); 
    

没有我可以将数据写入该数据库的任何问题,通过:

databaseReference.setValue( “2.06”);

但是当我从数据库中读取这个值时,它是一个空的String(null)。

如何?为什么?

- 编辑 - @Bob斯奈德的建议后 更新

databaseReference = firebaseDatabase.getReference("currentVersion"); 
    //databaseReference.setValue("2.06"); 
    databaseReference.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      // This method is called once with the initial value and again 
      // whenever data at this location is updated. 
      versionCheckFirebase = dataSnapshot.getValue(String.class); 
      Toast.makeText(MainActivity.this, versionCheckFirebase, Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
     public void onCancelled(DatabaseError error) { 
      // Failed to read value 
     } 
    }); 

这仍然显示一个空的烤面包的消息。 但是,当我更新Firebase中的数据时,它会直接在烤面包中显示数据。

我要的是: - 当我把这种方法,它应该在这个时候得到的数据,并用它做什么。但是现在只要我更新Firebase数据库,数据就会被调用/更新/检索。

- 编辑 - @Bob斯奈德的建议后更新

SOLUTION

得到它的工作:

public void getCurrentVersionFirebase(){ 
    databaseReference = firebaseDatabase.getReference("currentVersion"); 
    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      // This method is called once with the initial value and again 
      // whenever data at this location is updated. 
      String dataFromFirebase = dataSnapshot.getValue(String.class); 
      checkAppForUpdate(dataFromFirebase); 
     } 

     @Override 
     public void onCancelled(DatabaseError error) { 
      // Failed to read value 
     } 
    }); 
} 

和:

public void checkAppForUpdate(String dataFromFirebase){ 
    // do your stuff 
} 
+0

您是否拥有Firebase实时数据库的权限?它在另一个字符串上工作吗? –

+0

我看到它的工作,每当我更新数据库,但我想取/检索数据只有一次,每当我打电话来检索数据,并用它做的一些数据的方法。 –

+0

这听起来像你应该使用'addListenerForSingleValueEvent()',而不是'addValueEventListener()'。 –

回答

3

onDataChange()回调是异步的。在您当前的代码中,当您的语句显示Toast执行时,回调将不会运行。将您的吐司移入该方法:

@Override 
public void onDataChange(DataSnapshot dataSnapshot) { 
    // This method is called once with the initial value and again 
    // whenever data at this location is updated. 
    versionCheckFirebase = dataSnapshot.getValue(String.class); 
    Toast.makeText(this, versionCheckFirebase, Toast.LENGTH_SHORT).show(); 
} 
+0

仍然无法正常工作。请参阅顶端帖子中的编辑以获取更多相关信息。 –