This question already has answers here:
Firebase Database points to wrong database URL
(3 answers)
Closed 1 year ago.
Android Java.
When trying to reference my Realtime Database I'm using this to get the reference:
FirebaseDatabase rootDatabase;
DatabaseReference rootReference;
rootDatabase = FirebaseDatabase.getInstance();
Log.i("rootDatabase", String.valueOf(rootDatabase));
rootReference = rootDatabase.getReference("users");
Log.i("rootReference", String.valueOf(rootReference));
The URL shown in the console is different from that associated with my firebase project:
Output in the console
I/rootDatabase: com.google.firebase.database.FirebaseDatabase#<ProjectID>
I/rootReference: https://<Project-Name>-default-rtdb.firebaseio.com/users
When I open the URL from the console I get a page saying "not found".
But the URL that is shown above the database in the firebase console and in the google-services.json under firebase_url is:
https://<Project-Name>-default-rtdb.europe-west1.firebasedatabase.app/
Is there any way that I can either change the location of the Firebase database to no be in the Europe-west region or change some way to get the reference to the new server region? Many thanks.
When you are using the following line of code:
Log.i("rootDatabase", String.valueOf(rootDatabase));
You are passing to the valueOf() method the rootDatabase object, which is of type FirebaseDatabase. So you are only printing the String representation of the object, which is actually a memory address. However, when you are using this line:
Log.i("rootReference", String.valueOf(rootReference));
You are passing a different object to the valueOf() method. This time the object is of type DatabaseReference. Now the String representation of the object is actually a URL that points to your project.
Is there any way that I can either change the location of the firebase database to no be in the Europe-west region
Once you set the location, this cannot be changed. If you need another location, you need to create another project, set the location to be in another region than the Europe-west region, download the JSON file again and you'll be good to go.
Related
I'm trying to write messenger app using Firebase.
In database I have a few entries, which are User.class objects. I'm trying to write function which can download User object from database. I though that it'd be better to build separate class (UserManager) for this task, because I don't like making mess in code. But there is a problem, because in onCreate method I need to use User object to download some additional info from database to create conversation list, so downloading user from server should be done before that. Also if user is not in database, it should create and push User to database using FirebaseAuth (I've got that working).
Should I build class extending AsynchTask, and there put downloading user, and then updating UI with the data downloaded after user ?
How do I know if the user was already downloaded. Probably I should build some listener but I don't know how to do that.
Additional question:
If I use this reference with value listener, do i get a user object or some value from inside of the object?
DatabaseReference userReference = FirebaseDatabase.getInstance().getReference().child("users/" + mUserID);
Here is my database:
Each entry key is userID from FirebaseAuth for easier implementation.
I've been cracking my head on this for a few days and tried different approaches. I'll apriciate any help. I think, that some code or a scheme would be a huge help.
How do I know if the user was already downloaded?
You can add a flag to each user with the value of false and once you have downloaded the user object, to set the value to true but this is not how things are working with Firebase. You cannot know when a user from the database is completed downloaded becase Firebase is a realtime database and getting data might never complete. That's why is named a realtime database because in any momemnt the data under that User object can be changed, properties can be added or deleted.
You can use a CompletionListener only when you write or update data and you'll be notified when the operation has been acknowledged by the Database servers but you cannot use this interface when reading data.
If I use this reference with value listener, do i get a user object or some value from inside of the object?
If the value that you are listening to is a User object, then you'll get a User object. If the value is another type of object, which can also be a String (which is also an object) then you'll get that type of object, which can also be a String object. Remember, that only the keys in a Firebase database are always strings.
Maybe this part of my code will help you figure out:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users")
.child(mUserID);
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "DatabaseError: " + databaseError.getMessage());
}
});
I am trying to save location (so latitide and longitude) as one of the keys/fields in Firebase. In their example SFVehicles, they do show how to query once the information is stored but my problem is how do i save in the first place.
In their blog post, GeoFire goes Mobile, they are showing how the data would look like - but how do I get that location field populated?
I am able to save other types of strings to the Firebase though. I just use the code below.
Question is What data type should the location field be?
Firebase ref = new Firebase("https://myfirebaselink.firebaseio.com/");
//User
alan = new User("Alan Turing", 1912);
alanRef.setValue(obj);
I tried location to be a List<String>, but that did not work -- the location field looked like below:
Edit: On more research, found this blog post by Google but they are also saving as keys latitude1 and longitude. This probably was written before GeoFire` was introduced.
The GeoFire for Java project has a great README, that covers (amongst other) setting location data:
In GeoFire you can set and query locations by string keys. To set a location for a key simply call the setLocation() method. The method is passed a key as a string and the location as a GeoLocation object containing the location's latitude and longitude:
geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));
To check if a write was successfully saved on the server, you can add a GeoFire.CompletionListener to the setLocation() call:
geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973), new GeoFire.CompletionListener() {
#Override
public void onComplete(String key, FirebaseError error) {
if (error != null) {
System.err.println("There was an error saving the location to GeoFire: " + error);
} else {
System.out.println("Location saved on server successfully!");
}
}
});
To remove a location and delete it from the database simply pass the location's key to removeLocation:
geoFire.removeLocation("firebase-hq");
It looks from here
That the type of the object is GeoLocation , like in line 83.
I have a lot of geo data stored online with latitude and longitude and I'd like to use the distanceTo method instead of my own haversine formula.
So I need to put each record into a Location field, but here's my question: it requires a "provider" string. Why? What will Android do with that information?
for (ArrayList<String> item : Places_Data) {
Location itemloc = new Location("provider");
itemloc.setLatitude(latIn);
itemloc.setLongitude(lonIn);
//do something with my new location
}
From the source code for Location, it doesn't use the string to do anything meaningful. It just uses it to describe the Location internally. If you do Location#toString(), it prints out the co-ordinates, the provider and other details (accuracy, etc). That's all it's used for, internal description.
You can make the provider anything, as seen in this answer: Creating Android Location Object
Use correctly the way
package android.location.LocationManager
Location location = new Location(LocationManager.GPS_PROVIDER);
Use NETWORK_PROVIDER o GPS_PROVIDER
https://developer.android.com/reference/android/location/LocationManager.html#GPS_PROVIDER
https://developer.android.com/reference/android/location/LocationManager.html#NETWORK_PROVIDER
I'm trying o use method CopyIntoItems and add to uploaded file owner property. Field Owner should be type USER. am setting up it like this:
FieldInformation fieldInformationUser = new FieldInformation();
fieldInformationUser.setDisplayName("Owner");
fieldInformationUser.setInternalName("Owner");
fieldInformationUser.setType(FieldType.USER);
fieldInformationUser.setValue("domain//username");
I'm using this library: Sharepoint library link
If TEXT type field is updated in presented above way - it passes, but does't update field at SharePoint server. Problem occurs when i'm using type USER - server returns
Invalid data has been used to update the list item. The field you are trying to update may be read only.
WSDL specifies fieldType.USER as a string field. he question is, how this string should look like... Anyone knows?
You must make sure that the user exists in the users table in SharePoint. It may be that it exists in AD but it hasn't been added to SharePoint yet.
If it were C#, then you would first issue the EnsureUser command:
//C# CSOM code
SPUser user=web.EnsureUser(userName);
listItem[fieldName] = user;
You should search for a similar method in the library you're using
I'm trying to read the query arguments of the URL in client side Java code, but I can't figure out how to find the current URL in Java.
When I tried using httpServletRequest as recommended in this question, it says that it cannot be resolved and it doesn't offer adding an import statement.
I'm using Google Web Toolkit with Google App Engine.
Look at Window.Location:
public static class Window.Location
This class provides access to the browser's location's object. The location object contains information about the current URL and methods to manipulate it. Location is a very simple wrapper, so not all browser quirks are hidden from the user.
There are a number of methods to retrieve info about the URL, including one to get the whole thing (getHref()) or get the constituent components (e.g. getProtocol(), getHost(), getHostName(), etc).
Since you say you want to read the query arguments, you probably want one of these:
static java.lang.String getQueryString()
Gets the URL's query string.
static java.lang.String getParameter(java.lang.String name)
Gets the URL's parameter of the specified name
static java.util.Map<java.lang.String,java.util.List<java.lang.String>> getParameterMap()
Returns a Map of the URL query parameters for the host page; since changing the map would not change the window's location, the map returned is immutable.