【发布时间】:2021-09-20 00:43:00
【问题描述】:
我正在尝试访问我在 firebase 中的数据库以进行读写,但不知何故,Android Studio 用来执行此操作的 url 是错误的。当我复制并粘贴此 URL (https://xxxx-rtdb.firebaseio.com/) 时,Google 会说:
数据库位于不同的区域。请将您的数据库 URL 更改为 https://xxxx-rtdb.europe-west1.firebasedatabase.app
(看到两个 URL 都有我项目的名称) 然后它识别出我正在寻找某个数据库,但为什么 AndroidStudio 设置此 URL?
这是我尝试此连接的简单代码:
package com.truequeapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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 static android.content.ContentValues.TAG;
public class MainActivity extends AppCompatActivity {
Button btn;
EditText txt;
FirebaseDatabase mDataBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.textView);
btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crearUser();
}
});
}
private void crearUser() {
FirebaseDatabase database = FirebaseDatabase.getInstance();
System.out.println("Acceso a BD");
DatabaseReference myRef = database.getReference().child("nombre");
myRef.setValue("Nicolas");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
System.out.println("value = " + value);
Log.d(TAG, "Value is: " + value);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
System.out.println("failed to read value");
Log.w(TAG, "Failed to read value.", error.toException());
}
});
DatabaseReference mDatabase = database.getReference();
mDatabase.child("usuarios").push().child("nombre").setValue("Nicolas");
}
}
【问题讨论】:
-
考虑从数据库 URL 和您共享的图像中编辑/删除您的项目 ID。它被认为是 PII。
-
您能详细解释一下吗?什么是 PII?
-
PII 或个人身份信息是指仅通过查看就可以识别个人、公司或其他实体(在本例中为您的项目)的信息。考虑一下,使用您的项目 ID 或此处的显式数据库 URL,任何人都可以尝试读取您的数据库。这可能导致达到配额(如果您的项目在免费计划下)或巨额账单(如果您的项目在 Blaze 计划下)。一般来说,尽量避免发布此类信息。
-
谢谢。我应该模糊这些信息,对吧?
-
是的,模糊效果很好。
标签: android firebase firebase-realtime-database