我正在尝试从Firebase数据库获取没有setValue
的整个节点。我在Google上获得了各种代码,但没有任何效果。这是我的JSON:
myAppInfo.json
{
"AppInfo" : {
"AppName" : "MyDemo1",
"VersionName" : "1.0",
"VersionCode" : "1.1"
}
}
Firebase方法
public void AppversionCheck() {
Log.v(Constants.TAG, "AppversionCheck ");
//String UserId = ref.child("AppInfo").push().getKey();
ref = new Firebase(Config.FIREBASE_URL);
//AppInfo appInfo = new AppInfo();
//appInfo.setAppName("AppInfo");
//ref.child("AppInfo").child("AppName").setValue("App1");
ref.child("AppInfo")./*child(UserId).*/child("AppName").setValue("App1");
//mDatabase = FirebaseDatabase.getInstance().getReference("AppInfo");
ref.child("AppInfo")/*.child(UserId)*/.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.v(Constants.TAG, "FireBaseCalled 11");
//Getting the data from snapshot
//AppInfo appInfo = postSnapshot.getValue(AppInfo.class);
Log.v(Constants.TAG, "postSnapshot " + dataSnapshot.getChildren());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.v(Constants.TAG, "FireBaseCalled 22");
//Getting the data from snapshot
//AppInfo appInfo = postSnapshot.getValue(AppInfo.class);
Log.v(Constants.TAG, "postSnapshot " + dataSnapshot.getChildren());
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.v(Constants.TAG, "FireBaseCalled1 33");
//Getting the data from snapshot
//AppInfo appInfo = postSnapshot.getValue(AppInfo.class);
Log.v(Constants.TAG, "postSnapshot " + dataSnapshot.getChildren());
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
Log.v(Constants.TAG, "FireBaseCalled 44");
//Getting the data from snapshot
//AppInfo appInfo = postSnapshot.getValue(AppInfo.class);
Log.v(Constants.TAG, "postSnapshot " + dataSnapshot.getChildren());
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
答案 0 :(得分:1)
你走了。!希望这对你有帮助。!
import com.google.firebase.auth.FirebaseAuth;
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.ValueEventListener;
public class MainActivity extends AppCompatActivity
{
private FirebaseDatabase mdatabase;
private DatabaseReference mdatabaseRef;
public static List<AppInfo > appInfo_list =new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Main_Activity);
initFirebase();
AddEventFireBaseListner();
}
public void initFirebase() {
mdatabase = FirebaseDatabase.getInstance();
mdatabaseReference = mdatabase.getReference();
// storageRef = FirebaseStorage.getInstance().getReference();
}
public void AddEventFireBaseListner() {
mdatabaseRef.child("AppInfo").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e(TAG, "AppInfoNode:");
if (appInfo_list.size() > 0)
appInfo_list.clear();
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
AppInfo appInfo= postsnapshot.getValue(AppInfo.class);
if(postSnapshot.getKey().equals("AppName"))
{
Log.e(TAG, "NodeSnapShot:");
String AppNameStr=postSnapshot.getValue().toString();
Log.e(TAG, "AppName:"+AppNameStr);
}
if(postSnapshot.getKey().equals("VersionName"))
{
Log.e(TAG, "VersionName:");
String VersionName=postSnapshot.getValue().toString();
Log.e(TAG, "VersionName:"+VersionName);
}
if(postSnapshot.getKey().equals("VersionCode"))
{
Log.e(TAG, "VersionCode:");
String VersionCode=postSnapshot.getValue().toString();
Log.e(TAG, "VersionCode:"+VersionCode);
}//end of if
appInfo_list.add(appInfo);
// AppInfo modelClass=new AppInfo ();
// modelClass.setAppName(AppNameStr);
// modelClass.setAppVersionName(VersionName);
// modelClass.setAppVersionCode(VersionCode);
}
}
在每个节点循环结束时,您可以将其存储在ModelClass对象中。!