Skip to content

Instantly share code, notes, and snippets.

@dsjt
Created May 15, 2018 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsjt/3fa5084f8f42610d272e9cc201bba664 to your computer and use it in GitHub Desktop.
Save dsjt/3fa5084f8f42610d272e9cc201bba664 to your computer and use it in GitHub Desktop.
firebaseクラスっぽいやつ
package sample.hoge;
import java.io.FileInputStream;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
public class FirebaseCommunicator {
FirebaseApp app;
public void initialize(String jsonFileName, String url, String identifier) throws Exception {
FileInputStream serviceAccount = new FileInputStream(jsonFileName);
FirebaseOptions options = new FirebaseOptions.Builder().setServiceAccount(serviceAccount).setDatabaseUrl(url)
.build();
FirebaseApp.initializeApp(options, identifier);
app = FirebaseApp.getInstance(identifier);
// System.out.println(app);
}
public FirebaseApp getApp() {
return app;
}
}
package sample.hoge;
import java.io.FileInputStream;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.ChildEventListener;
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 App {
static String JSON_FILE_NAME = "???.json";
static String URL = "h???";
public static void main(String[] args) throws Exception {
FirebaseCommunicator sampleFc = new FirebaseCommunicator();
sampleFc.initialize(JSON_FILE_NAME, URL, "sample");
DatabaseReference reference = FirebaseDatabase.getInstance(sampleFc.getApp()).getReference("folder");
System.out.println(reference.getKey());
reference.limitToLast(1).addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
Object key = dataSnapshot.getKey();
Object value = dataSnapshot.getValue();
System.out.println("child event : " + key + " , " + value + " and " + prevChildKey);
}
public void onCancelled(DatabaseError arg0) {
}
public void onChildChanged(DataSnapshot arg0, String arg1) {
}
public void onChildMoved(DataSnapshot arg0, String arg1) {
}
public void onChildRemoved(DataSnapshot arg0) {
}
});
reference.limitToLast(1).addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
Object obj = dataSnapshot.getValue();
System.out.println("value event : " + obj);
}
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
int counter = 0;
while (true) {
counter++;
System.out.println("実行中です。");
Thread.sleep(5000);
if(counter % 3 == 2) {
reference.push().setValue("クロウカード" + counter);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment