I am trying to save data on Firebase realetime database from Java class with a main method. But it doesn't works.I followed the instructions in firebase website, but the data of realtime database didn't update one time. And there is no error messages. I don't know why.
The rules has been set already.
{
"rules": {
".read": true,
".write": true
}
}
The following is my code, please give some suggestions, thanks a lot!
public class Firebasetest {
private static DatabaseReference database;
public static void main(String[] args) throws InterruptedException {
// Initialize Firebase
final Semaphore semaphore = new Semaphore(0);
FirebaseApp defaultApp = null;
try {
// [START initialize]
FileInputStream serviceAccount = new FileInputStream("test-firebase.json");
FirebaseOptions options =
new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://test.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
System.out.println("init");
// [END initialize]
} catch (IOException e) {
System.out.println(e.getMessage());
System.out.println("ERROR: invalid service account credentials. See README.");
System.out.println(e.getMessage());
}
try {
database = FirebaseDatabase.getInstance().getReference();
ApiFuture<Void> future = database.setValueAsync("1234");
try {
// future.get();
future.get(20, TimeUnit.SECONDS);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
final CountDownLatch latch = new CountDownLatch(1);
database.setValue(
"234343",
new DatabaseReference.CompletionListener() {
#Override
public void onComplete(
DatabaseError databaseError, DatabaseReference databaseReference) {
latch.countDown();
}
});
System.out.println("done");
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Related
I need to synchronize the time on a Java client application with the Firebase server, to prevent fails when the client system time is different from the server. Is it possible?
Here's my Firebase class code that starts the application:
private static FirebaseOptions getOptions() throws Exception {
/* ... */
for (Map<String, FirebaseOptions> map : credentials) {
if (map.containsKey(uri)) {
return map.get(uri);
}
}
try {
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(getCredentials(uri))
.build();
/* ... */
return options;
} catch (Exception e) {
/* ... */
}
}
public Firebase(String projectId) {
try {
try {
app = FirebaseApp.getInstance(projectId);
} catch (IllegalStateException e) {
FirebaseOptions options = getOptions();
for (FirebaseApp fapp : apps) {
if (fapp.getOptions().equals(options)) {
app = fapp;
}
}
if (app == null) {
app = FirebaseApp.initializeApp(options, projectId);
apps.add(app);
}
}
} catch (Exception e) {
error = e;
}
}
I can request the server timestamp by HTTP request, but how to change the time in the Java client? I tried use the FirestoreOptions setClock, but it still uses the system time, and works only if the system time is ok:
ZonedDateTime server = getServerTime();
ZonedDateTime now = ZonedDateTime.now();
long offsetNano = server.getNano() - now.getNano();
long offsetMilli = server.toInstant().toEpochMilli() - now.toInstant().toEpochMilli();
try {
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(getCredentials(uri))
.setFirestoreOptions(
FirestoreOptions
.newBuilder()
.setClock(new ApiClock() {
#Override
public long nanoTime() {
return ZonedDateTime.now().getNano() + offsetNano;
}
#Override
public long millisTime() {
return ZonedDateTime.now().toInstant().toEpochMilli() + offsetMilli;
}
})
.build()
)
.build();
I'm trying to get some information from Firebase Real-time database without success. I don't know what I'm doing wrong. I also tried the doc's example and they didn't work. Here's my code and my firebase db structue:
Topics.java:
public class Topics {
private String name;
public Topics() {
}
public Topics(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Main.java
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream serviceAccount;
FirebaseOptions options = null;
try {
serviceAccount = new FileInputStream(".//...");
options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("...")
.build();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
FirebaseApp.initializeApp(options);
String topics = getDatafromFirebase();
System.out.println("Everything right!");
}
private static String getDatafromFirebase() {
CountDownLatch done = new CountDownLatch(1);
StringBuilder b = new StringBuilder();
DatabaseReference dbRef = FirebaseDatabase.getInstance()
.getReference();
dbRef.child("topics").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
// TODO Auto-generated method stub
if(snapshot.exists()) {
for(DataSnapshot s:snapshot.getChildren()) {
Topics t = s.getValue(Topics.class);
b.append(t.getName());
b.append(" ");
done.countDown();
}
}
else {
b.append("No existe ");
done.countDown();
}
}
#Override
public void onCancelled(DatabaseError error) {
// TODO Auto-generated method stub
b.append("Error: "+error.getDetails());
done.countDown();
}
});
try {
done.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b.toString();
}
I have wait for the CountDownLatch for 5+ minutes, which I think is enough time for it to trigger. Also, important note: I have successfully sent message through firebase cloud messaging, so I don't think that it is a problem with the credentials.
I ran your code against my database with the same db structure and I can for sure say that I am ableto get information from the database.
onDataChange breakpoint not triggering only happens if I remove the topics subtree entirely. ie. in your case an empty database.
I suspect either your database url or Private Key JSON.
Follow the below Instruction for a new private key
In the console, click the gear icon on the left, and the Service Accounts Tab
Refer
Take a note of databaseUrl and click on Generate New Private Key, save it.
Refer
Here is the working code for example
package fireb;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import com.google.auth.oauth2.GoogleCredentials;
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;
public class Fireb {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream serviceAccount;
FirebaseOptions options = null;
try {
serviceAccount = new FileInputStream("C:\\key\\testapp-f0fe2-firebase-adminsdk-4po4a-5ce6c60b81.json");
options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://testapp-f0fe2.firebaseio.com")
.build();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
FirebaseApp.initializeApp(options);
String topics = getDatafromFirebase();
System.out.println(topics);
System.out.println("Everything right!");
}
private static String getDatafromFirebase() {
CountDownLatch done = new CountDownLatch(1);
StringBuilder b = new StringBuilder();
DatabaseReference dbRef = FirebaseDatabase.getInstance()
.getReference();
dbRef.child("topics").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
// TODO Auto-generated method stub
if(snapshot.exists()) {
for(DataSnapshot s:snapshot.getChildren()) {
Topics t = s.getValue(Topics.class);
b.append(t.getName());
b.append(" ");
}
done.countDown();
}
else {
b.append("No existe ");
done.countDown();
}
}
#Override
public void onCancelled(DatabaseError error) {
// TODO Auto-generated method stub
b.append("Error: "+error.getDetails());
done.countDown();
}
});
try {
done.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b.toString();
}
}
According to the documentation available here.
Before you can access the Firebase Realtime Database from a server using the Firebase Admin SDK, you must authenticate your server with Firebase. When you authenticate a server, rather than sign in with a user account's credentials as you would in a client app, you authenticate with a service account which identifies your server to Firebase.
If you are not running your code on server, you may authenticate as a client instead as described here.
Hope this helps.
I am following the documentation on Firebase website on setting up an Java Admin SDK. So I add the dependency to the build.gradle, and added the following code:
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.auth.FirebaseCredentials;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class Main {
public static void main(String[] args) throws FileNotFoundException{
FileInputStream serviceAccount = new FileInputStream("/Users/idanaviv10/Desktop/mapit-33290-firebase-adminsdk-fdy24-a1d0505493.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://mapit-33290.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog");
DatabaseReference usersRef = ref.child("users");
Map<String, User> users = new HashMap<String, User>();
users.put("alanisawesome", new User("June 23, 1912", "Alan Turing"));
users.put("gracehop", new User("December 9, 1906", "Grace Hopper"));
usersRef.setValue(users);
}
public static class User {
public String date_of_birth;
public String full_name;
public String nickname;
public User(String date_of_birth, String full_name) {
// ...
}
public User(String date_of_birth, String full_name, String nickname) {
// ...
}
}
}
The problem is when I am trying to get data (adding a listener) or trying to save data (like in the example) nothing happened. I am running the code in eclipse, I can see in the console that it print "Done", but when I check the database (on the firebase console on the browser) nothing change.
I found the problem, the problem was that the program will terminate before connecting to the Firebase server. What you need to do is to delay the termination of the program, by either calling Thread.sleep(20000);, or like I did it
Scanner scanner = new Scanner(System.in);
while(!scanner.nextLine().equals("Quit")){}
System.exit(0);
One other thing you can try is to explicitly wait until the setValue() task is complete using the Tasks API:
Task<Void> task = usersRef.setValue(users);
try {
Tasks.await(task);
} catch (ExecutionException | InterruptedException e) {
// Handle error if necessary
}
below code is pushing data into firebase database using java async call api from firebase sdk but listeners code is not executing . I am running below code in server side backend.
public enum Firebase {
INSTANCE;
FirebaseApp firebaseApp;
public void initilizeFirebaseApp(ConfigLoader configReader) {
CountDownLatch done = new CountDownLatch(1);
final AtomicInteger message1 = new AtomicInteger(0);
InputStream firebaseSecret = getClass().getClassLoader().getResourceAsStream("ServiceAccount.json");
final GoogleCredentials credentials;
try {
credentials = GoogleCredentials.fromStream(firebaseSecret);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error while reading Firebase config file." + e.toString());
throw new IllegalStateException(e);
}
Map<String, Object> auth = new HashMap<>();
auth.put("uid", "my_resources");
FirebaseOptions options = new Builder()
.setConnectTimeout(1000)
.setCredentials(credentials)
.setDatabaseAuthVariableOverride(auth)
.setDatabaseUrl(configReader.getFirebaseDatabaseURL())
.setStorageBucket(configReader.getFirebaseStorageBucket())
.setProjectId(configReader.getFirebseProjectId())
.build();
firebaseApp = FirebaseApp.initializeApp(options);
System.out.println(firebaseApp.getName());
//System.out.println(firebaseApp.getName());
// Use the shorthand notation to retrieve the default app's service
FirebaseAuth defaultAuth = FirebaseAuth.getInstance();
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();
// The app only has access as defined in the Security Rules
DatabaseReference ref = FirebaseDatabase
.getInstance()
.getReference("/analyst_profiles");
DateTime dt = new DateTime(java.util.Date.from(Instant.now()), java.util.TimeZone.getDefault());
System.out.println(dt.getValue());
//test data push
// https://firebase.google.com/docs/database/admin/save-data
AnalystProfiles analystProfilesObjTemp = new AnalystProfiles("test2", Long.toString(dt.getValue()), "dsds", "ds", "ds",
"dsa2323", "32ddss232");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
AnalystProfiles post = dataSnapshot.getValue(AnalystProfiles.class);
System.out.println(post);
//done.countDown();
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
CompletableFuture<String> welcomeText = CompletableFuture.supplyAsync(() -> {
try {
ref.push().setValueAsync(analystProfilesObjTemp).get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException("Error while waiting for future", e);
}
return "ok";
}).thenApply(x -> {
System.out.println(x);
System.out.println("Listeners code is not executing");
return x;
});
done.countDown();
try {
System.out.println(welcomeText.get());
done.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void testdataLoad() {
// The app only has access as defined in the Security Rules
DatabaseReference ref = FirebaseDatabase
.getInstance()
.getReference("/analyst_profiles");
DateTime dt = new DateTime(java.util.Date.from(Instant.now()), java.util.TimeZone.getDefault());
System.out.println(dt.getValue());
//test data push
// https://firebase.google.com/docs/database/admin/save-data
AnalystProfiles analystProfilesObjTemp = new AnalystProfiles("test2", Long.toString(dt.getValue()), "dsds", "ds", "ashutsh",
"dsa2323", "32ddss232");
CompletableFuture<String> welcomeText = CompletableFuture.supplyAsync(() -> {
try {
ref.push().setValueAsync(analystProfilesObjTemp).get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException("Error while waiting for future", e);
}
return "ok";
}).thenApply(x -> {
System.out.println(x);
return x;
});
try {
System.out.println(welcomeText.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Add this code:
ApiFuture api=/*your reference to database*/usersRef.setValueAsync(users/*the data*/);
Object o = api.get(/*time out*/8,TimeUnit.SECONDS/*unite for time out her is seconds*/);
And at last you can check with
api.isDone();
I am trying to create sample java application to implement the MultiUserChat of XMPP. Some how I can able to create user and make it online in openfire. Can any one suggest how to join all the users to the created chatRoom?
Here is my sample code inside the class SampleMultiUserChat Where I invite all the users to join the group but it is not getting joined. What I am missing?
SampleMultiUserChat(){
oConnectionConfiguration = new ConnectionConfiguration("10.10.1.105",5223);
createChatRoom();
}
/**
* #param args
*/
public static void main(String[] args) {
SampleMultiUserChat oSampleMultiUserChat = new SampleMultiUserChat();
for(int i = 2; i < 4; i++){
oSampleMultiUserChat.openXMPPConnection("user"+i);
oSampleMultiUserChat.createAcceptInvitationListener("user"+i);
oSampleMultiUserChat.inviteToJoinRoom("user"+i);
}
Thread mainThread = Thread.currentThread();
while(true){
try {
mainThread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void openXMPPConnection(String user){
XMPPConnection oXmppConnection = new XMPPConnection(oConnectionConfiguration);
try {
oXmppConnection.connect();
oXmppConnection.login(user, "60474c9c10d7142b7508ce7a50acf414");
userConnection.put(user, oXmppConnection);
} catch (XMPPException e) {
System.out.println("Exception occured in login in user : "+user);
}
}
private void createChatRoom(){
XMPPConnection oXmppConnection = new XMPPConnection(oConnectionConfiguration);
try {
oXmppConnection.connect();
oXmppConnection.login("user1", "60474c9c10d7142b7508ce7a50acf414");
myChattingRoom = new MultiUserChat(oXmppConnection, "mychattingroom#conference.10.10.1.105");
myChattingRoom.create("roomNickName");
myChattingRoom.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
} catch (XMPPException e) {
e.printStackTrace();
}
}
private void inviteToJoinRoom(String user){
myChattingRoom.invite(user+"#10.10.1.105", "Please join my chatting room");
System.out.println("sent invitation by "+user);
}
private void sendMessage(String msg){
try {
myChattingRoom.sendMessage(msg);
} catch (XMPPException e) {
System.out.println("Exception occured while sending msg to chat room"+e);
}
}
private void createAcceptInvitationListener(String user){
MultiUserChat.addInvitationListener(userConnection.get(user), new InvitationListener() {
public void invitationReceived(Connection connection, String room, String inviter,
String reason, String password, Message msg) {
try {
myChattingRoom.join(connection.getUser().substring(0, connection.getUser().indexOf("#")));
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
}
Thanks in advance.
I solved my above problem by creating new instance of MultiUserChat.
Here is my edited method 'createAcceptInvitationListener'
private void createAcceptInvitationListener(String user){
System.out.println("inside create accept invitation listener");
final XMPPConnection oXmppConnection = userConnection.get(user);
MultiUserChat.addInvitationListener(oXmppConnection, new InvitationListener() {
public void invitationReceived(Connection connection, String room, String inviter,
String reason, String password, Message msg) {
System.out.println("inside invitation received method");
try {
System.out.println(connection.getUser().substring(0, connection.getUser().indexOf("#")));
MultiUserChat myChattingRoom = new MultiUserChat(oXmppConnection, "mychattingroom#conference.10.10.1.105");
myChattingRoom.join(connection.getUser().substring(0, connection.getUser().indexOf("#")));
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception occured while joining the chat room : "+e);
}
}
});
}
private void reservedRoomsCreation(MultiUserChat myChattingRoom) throws XMPPException{
Form form = myChattingRoom.getConfigurationForm();
Form submitForm = form.createAnswerForm();
for(Iterator fields = form.getFields(); fields.hasNext();){
FormField formFields = (FormField) fields.next();
if (!FormField.TYPE_HIDDEN.equals(formFields.getType()) && formFields.getVariable() != null) {
submitForm.setDefaultAnswer(formFields.getVariable());
}
}
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
myChattingRoom.sendConfigurationForm(submitForm);
}
I've succeeded onto transferring files onto my Dropbox account.
But when i share my app for testing with other members of the project, they can't transfer the files on their own Dropbox account.
And when they test the functionality, it transfers automatically onto my account.
Is there something I'm missing out here?
I know that i don't need to add manually users, they can automatically transfer the files they want.
Here's a snip of the method i used :
private void UploadToDropboxFromPath(String uploadPathFrom, String uploadPathTo) {
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
final String uploadPathF = uploadPathFrom;
final String uploadPathT = uploadPathTo;
Thread th = new Thread(new Runnable() {
public void run() {
File tmpFile = null;
try {
tmpFile = new File(uploadPathF);
} catch (Exception e) {
e.printStackTrace();
}
FileInputStream fis = null;
try {
fis = new FileInputStream(tmpFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
dropboxAPI.putFileOverwrite(uploadPathT, fis, tmpFile.length(), null);
} catch (Exception e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
}
});
th.start();
}