我试图使用firebase和android将数据输入数据库,但数据没有被输入数据库

时间:2017-09-06 17:36:33

标签: java android firebase-realtime-database

MainActivity文件

附加我的MainActivity ..无法找到应用程序成功构建的错误位置,但数据未显示在数据库中,同样为方便所有用户授予数据库权限,以后可以改变了...请帮忙

附加我的MainActivity ..无法找到应用程序成功构建的错误位置,但数据未显示在数据库中,同样为方便所有用户授予数据库权限,以后可以改变了...请帮忙

    package com.google.firebase.udacity.friendlychat;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.Editable;
    import android.text.InputFilter;
    import android.text.TextWatcher;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.ListView;
    import android.widget.ProgressBar;

    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;

    import java.util.ArrayList;
    import java.util.List;

    public class MainActivity extends AppCompatActivity {

        private static final String TAG = "MainActivity";

        public static final String ANONYMOUS = "anonymous";
        public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000;

        private ListView mMessageListView;
        private MessageAdapter mMessageAdapter;
        private ProgressBar mProgressBar;
        private ImageButton mPhotoPickerButton;
        private EditText mMessageEditText;
        private Button mSendButton;

        private String mUsername;

        private FirebaseDatabase mFirebaseDatabase;//firebase database object.It is an entry point to access the database
        private DatabaseReference mMessagesDatabaseReference;//is a class that references the specific part of the database.it will be referencring the messaging portion of the database


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

            mUsername = ANONYMOUS;

           // mFirebaseDatabase=FirebaseDatabase.getInstance();//main access for our database
            //mMessagesDatabaseReference=mFirebaseDatabase.getReference().child("messages");//using this we are getting the acess to messages section of the database
            mFirebaseDatabase = FirebaseDatabase.getInstance();
            mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages");

            // Initialize references to views
            mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
            mMessageListView = (ListView) findViewById(R.id.messageListView);
            mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
            mMessageEditText = (EditText) findViewById(R.id.messageEditText);
            mSendButton = (Button) findViewById(R.id.sendButton);

            // Initialize message ListView and its adapter
            List<FriendlyMessage> friendlyMessages = new ArrayList<>();
            mMessageAdapter = new MessageAdapter(this, R.layout.item_message, friendlyMessages);
            mMessageListView.setAdapter(mMessageAdapter);

            // Initialize progress bar
            mProgressBar.setVisibility(ProgressBar.INVISIBLE);

            // ImagePickerButton shows an image picker to upload a image for a message
            mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // TODO: Fire an intent to show an image picker
                }
            });

            mSendButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Send messages on click
                    //FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null);
                    //mMessagesDatabaseReference.push().setValue(friendlyMessage);
                    //above statement is used to send the data to the cloud

                    FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null);
                    mMessagesDatabaseReference.push().setValue(friendlyMessage);


                    // Clear input box
                    mMessageEditText.setText("");
                }
            });
            // Enable Send button when there's text to send
            mMessageEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if (charSequence.toString().trim().length() > 0) {
                        mSendButton.setEnabled(true);
                    } else {
                        mSendButton.setEnabled(false);
                    }
                }

                @Override
                public void afterTextChanged(Editable editable) {
                }
            });
            mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)});

            // Send button sends a message and clears the EditText
            mSendButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // TODO: Send messages on click

                    // Clear input box
                    mMessageEditText.setText("");
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main_menu, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            return super.onOptionsItemSelected(item);
        }
    }

1 个答案:

答案 0 :(得分:0)

确保您已设置所需的依赖关系,请按照https://firebase.google.com/docs/android/setup

上的最新指南进行操作

或者在Android Studio中,您只需使用工具导入应用程序 - &gt; Firebase - &gt; [前。实时数据库] [import app using android studio's built-in tool] 1

使用数据库规则:

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

不授予所有用户权限,此规则只授予未经过身份验证的用户读/写。

解决方案:将读/写规则设置为true,仅进行测试(直到使用Firebase身份验证)

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