0

(注:ユーザーがアクティビティを表示するにはログインする必要があり、それが実装され、作業罰金です)なぜFirebaseデータベースからデータを取得するのがユーザーの認証ルールで機能しないのですか?しかし、匿名ルールを使用するのはなぜですか?

(UPDATE:私は、ネストされたプッシュ下の値を取得するには苦労他人を助ける願って作業コードに私のコードを更新しましたfirebase push()によって生成されたユニークなキー - 私が意味するものを得るためにUser_locationの下の画像を確認してください)

私は皆さんが私を得ることを保証するために非常に詳しく説明します。すべての まず、私は(それは完全に罰金働いたと私はFirebaseから自分のデータを取得し、それらを見ることができました。)、次のようにデータベースルールを設定することにより、ユーザー認証なしで私のコードを試してみました

{ 
    "rules": { 

     ".write": true, 
     ".read": true 
     } 
    } 

そして、I

11-21 04:20:01.424 9820-9820/? W/zygote: Unexpected CPU variant for X86 using defaults: x86 
11-21 04:20:01.453 9820-9828/? E/zygote: Failed sending reply to debugger: Broken pipe 
11-21 04:20:01.951 9820-9867/? W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found. 
11-21 04:20:01.978 9820-9867/? W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found. 
11-21 04:20:02.081 9820-9867/? W/zygote: Skipping duplicate class check due to unrecognized classloader 
11-21 04:20:02.700 9820-9876/com.example.msiuser.atyourservice W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 
11-21 04:20:05.139 9820-9820/com.example.msiuser.atyourservice W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1 
11-21 04:20:05.303 9820-9820/com.example.msiuser.atyourservice W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1 

                       [ 11-21 04:20:05.342 9820: 9876 D/   ] 
                       SurfaceInterface::setAsyncMode: set async mode 1 
11-21 04:20:05.482 9820-9820/com.example.msiuser.atyourservice W/View: requestLayout() improperly called by android.widget.ListView{337754f VFED.VC.. .F....ID 0,207-1080,1260 #7f080079 app:id/list} during layout: running second layout pass 
)アプリケーションがクラッシュしない(と活動を示す空の白い画面:これは、エラーメッセージが

{ 
    "rules": { 
    "users": { 
     "$uid": { 
     ".write": "$uid === auth.uid", 
     ".read": "$uid === auth.uid" 
     } 
    } 
    } 
} 

である)以下の:(私が得たエラーとして私のルールを設定します

ここに私のコードです:(layout.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <ListView 
     android:id="@+id/lv" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 
</LinearLayout> 

私のJavaクラス

publicクラスのアドレス帳はAppCompatActivity { プライベート静的最終文字列タグ= "アドレス帳" を拡張します。

FirebaseDatabase database; 
DatabaseReference myRef; 
DatabaseReference myRef2; 
private String userID; 
private ListView liv; 
private ArrayList<String> user_addresses = new ArrayList<>(); 
private ArrayAdapter<String> adapter; 
private FirebaseAuth mAuth; 
private FirebaseAuth.AuthStateListener mAuthListener; 

@Override 
protected void onCreate(Bundle savedIntancesState) { 

    super.onCreate(savedIntancesState); 
    setContentView(R.layout.activity_addressbook_listview); 

    liv = (ListView) findViewById(R.id.lv); 
    mAuth = FirebaseAuth.getInstance(); 
    database= FirebaseDatabase.getInstance(); 
    myRef = database.getReference("User_Location"); 
    final FirebaseUser user = mAuth.getCurrentUser(); 


    mAuthListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser user = firebaseAuth.getCurrentUser(); 
      if (user != null) { 
       Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); 

       adapter = new ArrayAdapter<String>(AddressBook.this, android.R.layout.simple_list_item_1, user_addresses); 
       liv.setAdapter(adapter); 
       myRef.addChildEventListener(new ChildEventListener() { 
        @Override 
        //How to retireve data under nested push unique key that made by firebase.. 

        public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
         for (DataSnapshot location : dataSnapshot.getChildren()) { 
          String value = location.child("location").getValue(String.class); 
          user_addresses.add(value); 
          adapter.notifyDataSetChanged(); 
         } 

        } 

        @Override 
        public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

        } 

        @Override 
        public void onChildRemoved(DataSnapshot dataSnapshot) { 
         for (DataSnapshot location : dataSnapshot.getChildren()) { 
          String value = location.child("location").getValue(String.class); 
          user_addresses.remove(value); 
          adapter.notifyDataSetChanged(); 
         } 
        } 

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

        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       }); 
       // User is signed in 
      } else { 
       // User is signed out 
       Log.d(TAG, "onAuthStateChanged:signed_out"); 
       toastMessage("Successfully signed out."); 
      } 
      // ... 
     } 
    }; 


    } 

@Override 
public void onStart() { 
    super.onStart(); 
    mAuth.addAuthStateListener(mAuthListener); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    if (mAuthListener != null) { 
     mAuth.removeAuthStateListener(mAuthListener); 
    } 
} 



private void toastMessage(String message){ 
    Toast.makeText(this,message,Toast.LENGTH_SHORT).show(); 
} 

}

だから、基本的には上記のコードで見ることができるよう、私は子供の子供を取得しようとしている「USER_LOCATION」、それはユーザー独自のUIDを含んでおり、それが子供の場所一意のIDです。ここではそれを明確にするために、私は私のデータを構造化する方法:

の1-ユーザー:

enter image description here

2-場所:

enter image description here

3-USER_LOCATION(自分で各ユーザーにリンク1人のユーザーが複数の場所を保存することができるため、ローン)

enter image description here

、私が間違っていたところを教えてください。..

enter image description here

答えて

0

をあなたのコードはmyRef = database.getReference("User_Location")に取り付けるが、あなたのルールは、/users/$uid読み権限を与えてください。したがって、読み込みは拒否されます。

リスナーを機能させるには、リスナーを接続する場所をユーザーが読み取る権限を持っていることを確認します。だから、:

{ 
    "rules": { 
    "User_Location": { 
     ".read": "auth.uid !== null" 
    } 
    } 
} 

あなたはこれを試してみたくなるかもしれません:あなたは/User_Locationに読み取り権限を付与していないので、

{ 
    "rules": { 
    "User_Location": { 
     "$uid": { 
     ".write": "$uid === auth.uid", 
     ".read": "$uid === auth.uid" 
     } 
    } 
    } 
} 

しかし/User_Locationにリスナーを取り付けるには、まだ、上記のルールで拒否されます。このようにデータをフィルタリングするためにセキュリティルールを使用するのは一般的ですが、rules are not filtersです。詳細については、多くのquestions about this topicの一部をお読みください。

+0

説明してくれてありがとう.. – GeishaZ11

関連する問題