在 Firebase 数据库中读取和写入数据

Posted

技术标签:

【中文标题】在 Firebase 数据库中读取和写入数据【英文标题】:Reading and writing data in Firebase Database 【发布时间】:2017-10-12 06:34:12 【问题描述】:

注意:- 抱歉我的英语不好,因为英语不是我的母语,我为此使用了谷歌翻译器。

我正在开发一个 android Map 应用程序,用户可以在其中找到他们朋友的当前位置并存储我使用 FireBase 实时数据库的当前位置。因此,当用户点击查找按钮时,他们朋友的当前位置将在地图上显示他们的用户名。

但是当我尝试从 firebase 获取用户数据时,我无法解决一个问题。它没有以正确的方式反映。数据反映在日志中的 2 行不同的行中:

第 1 行:- 纬度和经度为 0.0,但电子邮件显示在中 第 2 行:- 纬度和经度反映正确,但电子邮件为空

我不知道如何将两者合二为一。我在谷歌上搜索了很多但失败了。请帮助我了解如何以正确的方式完成。

以下是我的代码和 android studio 屏幕截图。

MainActivity.java

    findfriend.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            startActivity(new Intent(MainActivity.this, MapsActivity.class));
        
    );

    final TextView tname;
    tname = (TextView) findViewById(R.id.textview);

    ref = FirebaseDatabase.getInstance().getReferenceFromUrl("https://ffinder-b4617.firebaseio.com/Email");
    mReferece2 = ref.child("email");
    mReferece2 = ref.child("location");

    proceed.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 

            ref.addValueEventListener(new ValueEventListener() 
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) 
                    for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                        UserInformation details = dataSnapshot1.getValue(UserInformation.class);

                        String email = details.email;
                        Double lat = details.latitude;
                        Double lon = details.longitude;
                        tname.setText(lat+" "+'\n'+lon+'\n'+email);

                        System.out.println("-->"+ lat+" " + lon+" "+ email);
                    
                

                @Override
                public void onCancelled(DatabaseError databaseError) 

                
            );

        
    );

MapsActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        Firebase.setAndroidContext(this);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
            checkLocationPermission();
        

        //Check if Google Play Services Available or not
        if (!CheckGooglePlayServices()) 
            Log.d("onCreate", "Finishing test case since Google Play Services are not available");
            finish();
         else 
            Log.d("onCreate", "Google Play Services available.");
        

        auth = FirebaseAuth.getInstance();

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

 FirebaseDatabase.getInstance().getReference().child("Location");
    

    @Override
    public void onLocationChanged(Location location) 
        Log.d("onLocationChanged", "entered");

        mLastLocation = location;

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = new Date();
        mLastUpdateTime = ((dateFormat.format(date).toString()));

        saveToFirebase();

        if (mCurrLocationMarker != null) 
            mCurrLocationMarker.remove();
        

        latitude = location.getLatitude();
        longitude = location.getLongitude();

        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.draggable(true);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mCurrLocationMarker = mMap.addMarker(markerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

        Toast.makeText(MapsActivity.this,"Your Current Location", Toast.LENGTH_LONG).show();

        //stop location updates
        if (mGoogleApiClient != null) 
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            Log.d("onLocationChanged", "Removing Location Updates");
        

    

    public void saveToFirebase() 

        Firebase firebase = new Firebase("https://ffinder-b4617.firebaseio.com").child("Email").child("location");

        Map mLoactions = new HashMap();
        mLoactions.put("timestamp",mLastUpdateTime);
        mLoactions.put("latitude", mLastLocation.getLatitude());
        mLoactions.put("longitude", mLastLocation.getLongitude());

        firebase.setValue(mLoactions);
    

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) 


    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
    public boolean checkLocationPermission()
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) 

            // Asking user if explanation is needed
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)) 

                ActivityCompat.requestPermissions(this,
                        new String[]Manifest.permission.ACCESS_FINE_LOCATION,
                        MY_PERMISSIONS_REQUEST_LOCATION);


             else 
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]Manifest.permission.ACCESS_FINE_LOCATION,
                        MY_PERMISSIONS_REQUEST_LOCATION);
            
            return false;
         else 
            return true;
        
    

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) 
        switch (requestCode) 
            case MY_PERMISSIONS_REQUEST_LOCATION: 
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) 

                    if (ContextCompat.checkSelfPermission(this,
                            Manifest.permission.ACCESS_FINE_LOCATION)
                            == PackageManager.PERMISSION_GRANTED) 

                        if (mGoogleApiClient == null) 
                            buildGoogleApiClient();
                        
                        mMap.setMyLocationEnabled(true);
                    

                 else 

                    // Permission denied, Disable the functionality that depends on this permission.
                    Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
                
                return;
            
        
    

    @Override
    public boolean onMarkerClick(Marker marker) 
        marker.setDraggable(true);
        return false;
    

    @Override
    public void onMarkerDragStart(Marker marker) 

    @Override
    public void onMarkerDrag(Marker marker) 

    @Override
    public void onMarkerDragEnd(Marker marker) 
        end_latitude = marker.getPosition().latitude;
        end_longitude =  marker.getPosition().longitude;

        Log.d("end_lat",""+end_latitude);
        Log.d("end_lng",""+end_longitude);
    

Login.class

@Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Firebase.setAndroidContext(this);

        auth = FirebaseAuth.getInstance();

        if (auth.getCurrentUser() != null)
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        

        setContentView(R.layout.activity_login);

        inputEmail = (EditText) findViewById(R.id.email);
        inputPassword = (EditText) findViewById(R.id.password);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        btnSignup = (Button) findViewById(R.id.btn_signup);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnReset = (Button) findViewById(R.id.btn_reset_password);

        auth = FirebaseAuth.getInstance();

        btnSignup.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                startActivity(new Intent(LoginActivity.this, SignupActivity.class));
            
        );

        btnReset.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
            
        );

        btnLogin.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                String email = inputEmail.getText().toString();
                final String password = inputPassword.getText().toString();

                if (TextUtils.isEmpty(email)) 
                    Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                    return;
                

                if (TextUtils.isEmpty(password)) 
                    Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                    return;
                
         progressBar.setVisibility(View.VISIBLE);

                //authenticate user
                auth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() 
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) 

                                progressBar.setVisibility(View.GONE);
                                if (!task.isSuccessful()) 
                                    // there was an error
                                    if (password.length() < 6) 
                                        inputPassword.setError(getString(R.string.minimum_password));
                                     else 
                                        Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                                    
                                 else 
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
       
      
    );
  
 );
 

    public void userLoginInfo()

      String email = inputEmail.getText().toString();

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Email").push();
        String userId = mDatabase.child("email").getKey();
        mDatabase.child(userId).setValue(email);
    

UserInformation.class

public class UserInformation 

    public String email;
    public double latitude;
    public double longitude;

    public UserInformation() 
    

    public UserInformation(String email, double latitude, double longitude) 
        this.email = email;
        this.latitude = latitude;
        this.longitude = longitude;
    

    public String getEmail() 
        return email;
    

    public double getLatitude() 
        return latitude;
    

    public double getLongitude() 
        return longitude;
    

Firebase 数据库截图 Android Studio 日志截图

【问题讨论】:

【参考方案1】:

您的userLoginInfo() 方法使用push() 在数据库中创建一个唯一键来存储电子邮件地址,而您的saveToFirebase() 方法以不同方式存储纬度/经度值,这意​​味着数据最终位于不同的位置。

使用您的UserInformation 对象来标准化您的数据库并使用DatabaseReference#setValue(Object) 将是有益的 和DataSnapshot#getValue(Class&lt;CT&gt;) 存储和检索数据。因此,当您想要存储用户的位置时,您可以这样做:

public void saveToFirebase() 
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Email").push();

    String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();

    UserInformation userInformation = new UserInformation(email, mLastLocation.getLatitude(), mLastLocation.getLongitude());

    ref.setValue(userInformation);

然后,为了稍后检索这些详细信息,您可以使用现有的 ValueEventListener 并进行一些调整:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Email");

ref.addValueEventListener(new ValueEventListener() 
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) 
        for (DataSnapshot child : dataSnapshot.getChildren())
            UserInformation details = child.getValue(UserInformation.class);

            System.out.println("-->" + details.getLatitude() +" " + details.getLongitude() +" "+ details.getEmail());
        
    

    @Override
    public void onCancelled(DatabaseError databaseError) 
);

希望我正确理解了您的要求,如果我遗漏了什么,请告诉我。

【讨论】:

感谢您的回复,它帮助我在一个父级“电子邮件”下组织数据,但现在我在 Firebase 中没有收到用户的登录电子邮件。 如下图所示:- ffinder-b4617 电子邮件:“email” 纬度:31.5037501 经度:74.3502436 您可以在当前用户登录后使用FirebaseAuth.getInstance().getCurrentUser().getEmail()获取当前用户的电子邮件地址,因此您可以在saveToFirebase()方法中使用它。这行得通吗? 我已将此代码“FirebaseAuth.getInstance().getCurrentUser().getEmail()”放在 saveToFirebase() 中,但什么也没发生。 public void saveToFirebase() DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); UserInformation userInformation = new UserInformation("email", mLastLocation.getLatitude(), mLastLocation.getLongitude()); FirebaseAuth.getInstance().getCurrentUser().getEmail(); ref.setValue(userInformation);

以上是关于在 Firebase 数据库中读取和写入数据的主要内容,如果未能解决你的问题,请参考以下文章

Firebase 权限被拒绝(读取和写入)与身份验证

Firebase 数据库安全规则允许经过身份验证的用户读取和写入自己的内容

从登录用户创建的 Firebase 读取数据 - Flutter [关闭]

firebase 数据库规则允许读取/写入具有特定信息的特定用户

无法读取或写入 firebase 实时数据库(android studio)

无法通过 Unity 将数据写入 Firebase 数据库