拒绝从Firebase检索数据的访问

时间:2018-12-11 22:13:06

标签: java android firebase

我正在尝试从Firebase检索温度数据,但是什么也没出现。不确定问题是什么以及如何解决。 我感觉好像我的Firebase身份验证存在问题,并且能够授予它从数据库读取的权限。

错误提示:

W/RenderThread: type=1400 audit(0.0:12802165): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=15849 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0

TempHistory.java:

package com.example.dialg.projectlayouts;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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 com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.Query;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TempHistory extends AppCompatActivity {
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase database;
private DatabaseReference myRef;


private static final String TAG = "TempHistory";
DataStructure mData;
private TextView temperature;
   // private TextView timestamp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_temp_history);

    temperature = (TextView) findViewById(R.id.readTemperature);

    //Back button
    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle("Previous Saves");


    mAuth = FirebaseAuth.getInstance();
    database = FirebaseDatabase.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    String userID = user.getUid();


    myRef = database.getReference().child(userID).child("DataStructure");

    //Reads from Database Functions
    retrieveData();
    }


private void retrieveData(){
    myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            DataStructure ds = dataSnapshot.getValue(DataStructure.class);
            temperature.setText("Previous Temperature (in celsius): " +ds.getTemperature());

            //timestamp.setText(convertTimestamp(ds.getTimestamp()));
        }

      /*  private String convertTimestamp(String timestamp){

            long yourSeconds = Long.valueOf(timestamp);
            Date mDate = new Date(yourSeconds * 1000);
            DateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
            return df.format(mDate);
        }
            */

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, 
        @Nullable String s) {
            DataStructure ds = dataSnapshot.getValue(DataStructure.class);
            temperature.setText("Previous Temperature (in celsius): " 
            +ds.getTemperature());

          //  timestamp.setText(convertTimestamp(ds.getTimestamp()));

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
    myRef.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            List<DataStructure> arraylist = new ArrayList<>();
            if (dataSnapshot !=null && dataSnapshot.getValue() !=null){

                for (DataSnapshot a : dataSnapshot.getChildren()) {
                    DataStructure dataStructure = new DataStructure();
                    dataStructure.setTemperature(a.getValue(DataStructure.class).getTemperature());
                    arraylist.add(dataStructure);

                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}
}

数据结构:

package com.example.dialg.projectlayouts;

public class DataStructure {
private String temperature;
//private String timestamp;

public DataStructure() {

}


public DataStructure(String temperature) {
    this.temperature = temperature;
    //this.timestamp = timestamp;
}

public String getTemperature() {
    return temperature;
}

public void setTemperature(String temperature) {
    this.temperature = temperature;
}

}

我甚至更改了写入和读取数据库的规则以适合适当的凭据。

{
"rules": {
".read": "auth !=null",
".write": "auth !=null"
}
}

1 个答案:

答案 0 :(得分:0)

确保应用名称和客户端ID与Firebase控制台中的相同。

如果不确定从项目控制台重新下载google-services.json并将其添加到您的项目中。