티스토리 뷰

1. 파이어 베이스 데이터베이스를 사용하기 위해서는 app 수준 gradle 파일에 

compile 'com.google.firebase:firebase-database:9.6.1'

를 추가해 주어야 합니다.

https://firebase.google.com/docs/database/android/start/ 이나 콘솔창에서

compile 'com.google.firebase:firebase-database:10.0.0

compile 'com.google.firebase:firebase-database:10.0.0'

를 추가하라고 하지만 버전의 문제인지 build 되지 않습니다.


2. 이제 리얼타임 데이터베이스의 사용 권한을 설정하여야 합니다.

파이어 베이스 사이트에서 규칙을 수정해 주어야 합니다.

{

  "rules": {

    ".read": "auth != null",

    ".write": "auth != null"

  }

}

코드를

{

  "rules": {

    ".read": "true",

    ".write": "true"

  }

}

로 바꾸어 게시합니다



3. User 클래스를 생성합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.example.kim.test;
 
public class User {
    public String id;
    public String pw;
 
    public User() {
 
    }
 
    public User(String id, String pw) {
        this.id = id;
        this.pw = pw;
    }
 
    public String getId() {
        return id;
    }
 
    public String getPw() {
        return pw;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public void setPw(String pw) {
        this.pw = pw;
    }
}
cs


4. activity.xml 코드를 입력합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.kim.test.MainActivity">
 
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        >
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="User ID"
            android:textSize="20dp"
            />
 
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/user_id"
            />
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="User PW"
            />
 
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/user_pw"
            android:password="true"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Write DB"
            android:id="@+id/btn_writedb"
            android:layout_gravity="center_horizontal"
            android:onClick="onClick"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Read DB"
            android:id="@+id/btn_readdb"
            android:layout_gravity="center_horizontal"
            android:onClick="onClick"
            />
    </LinearLayout>
</RelativeLayout>
 
cs


5. MainActivity.java 소스를 입력합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.example.kim.test;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
 
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;
 
public class MainActivity extends AppCompatActivity {
    FirebaseDatabase database;
    DatabaseReference myRef;
    EditText txt_user_id, txt_user_pw;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        txt_user_id = (EditText) findViewById(R.id.user_id);
        txt_user_pw = (EditText) findViewById(R.id.user_pw);
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference();
 
    }
 
    public void onClick(View v){
        switch(v.getId()) {
            case R.id.btn_writedb:
                User userdata = new User(txt_user_id.getText().toString(), txt_user_pw.getText().toString());
                myRef.child("userdata").child(userdata.getId()).setValue(userdata);
 
                break;
            case R.id.btn_readdb:
                try {
                    myRef.child("userdata").child(txt_user_id.getText().toString()).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            User user = dataSnapshot.getValue(User.class);
                            Toast.makeText(MainActivity.this"id:"+user.getId()+", pw:"+user.getPw(), Toast.LENGTH_SHORT).show();
                        }
 
                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            Toast.makeText(MainActivity.this"fail", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch(Exception e) {
                    Toast.makeText(this"" + e.toString(), Toast.LENGTH_SHORT).show();
                }
 
 
                break;
        }
    }
}
 
cs


'파이어 베이스' 카테고리의 다른 글

파이어 애널러틱스  (0) 2016.11.24
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함