0

I'm new in java and firebase.

I'm just testing the firebase sdk server. I created a basic java class that only have to save a data each time i run it.

Please understand that i am not talking about firebase android client sdk but it's about the firebase server java sdk.

I'm following the firebase tutorial to save an item in a firebase tutorial but nothing happens and apparently no data are saved in my db.Yet, i get no error in eclipse console, so very hard to debug this.

Here is my code :

package firebaseTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
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;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpExecuteInterceptor;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.*;
import com.fasterxml.jackson.core.JsonFactory;
public class User {
  public static void main(String[] args) throws FileNotFoundException{
     FirebaseOptions options = new FirebaseOptions.Builder()
        .setDatabaseUrl("https://myfirebasedatabaseurl.firebaseio.com")
        .setServiceAccount(new FileInputStream("path/to/my/service/account/file.json"))
        .build();
    FirebaseApp.initializeApp(options); 
    
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference("server/saving-data/fireblog");
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            
            public void onDataChange(DataSnapshot dataSnapshot) {
                Object document = dataSnapshot.getValue();
                //System.out.println(document);
                
            }
            
            public void onCancelled(DatabaseError arg0) {
                // TODO Auto-generated method stub
                
            }
        }); 
    DatabaseReference usersRef = ref.child("users");

    Map<String, String> users = new HashMap<String, String>();
    users.put("user1", "foo");
    users.put("user2", "bar");

    usersRef.setValue(users);
    
  }
 
}

Can someone help me to understand what's wrong with my code ?

Thanks.

Community
  • 1
  • 1
John doe
  • 3,680
  • 7
  • 31
  • 65

0 Answers0