群聊应用程序

时间:2018-04-16 10:03:54

标签: android firebase firebase-realtime-database

我正在尝试使用udacity(Firebase在一个周末)使用firebase创建一个安卓群组聊天应用程序 我已经按照每一步,但我的数据不会进入数据库。 我和你分享我的代码,请告诉我哪里出错了。 我更改了firebase中的身份验证规则,并将它们设置为true以进行读写。

 mUsername = ANONYMOUS;
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mMessagesDatabaseRference = 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);
    mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null);
            mMessagesDatabaseRference.push().setValue(friendlyMessage);

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

这是我的build.gradle文件依赖项

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.google.firebase:firebase-database:12.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'implementation 'com.github.bumptech.glide:glide:4.6.1'
}

这是项目级gradle文件...     buildscript {

repositories {
    jcenter()
    mavenLocal()
    google()
}
dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

这是主要的活动布局文件......

 <ImageButton
        android:id="@+id/photoPickerButton"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:background="@android:drawable/ic_menu_gallery" />

    <EditText
        android:id="@+id/messageEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" />

    <Button
        android:id="@+id/sendButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:enabled="false"
        android:text="Send"/>

的Manifest.xml

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Base.Theme.AppCompat.Light">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

友情留言代码

public class FriendlyMessage {
    private String text;
    private String name;
    private String photoUrl;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;}
    }

Database structure should look like this

4 个答案:

答案 0 :(得分:0)

您是否尝试在清单上添加互联网权限?

    <uses-permission android:name="android.permission.INTERNET" />

如果这不起作用,您应该在执行期间提供更多相关信息作为logcat。实际上,如果数据库权限出错,Firebase数据库会在日志中显示错误消息。

答案 1 :(得分:0)

让FriendlyMessage模型成员公开!

public class FriendlyMessage {
    public String text;
    public String name;
    public String photoUrl;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;}
    }

答案 2 :(得分:0)

您需要将特定的数据库网址提供给FireDatabase,如下所示:

mFirebaseDatabase = FirebaseDatabase.getInstance("https://xxxx-d9b10.firebaseio.com");

将在此Firebase中找到该网址。如果您已经创建了项目,则选择您的项目。否则你应该创建项目。选择或创建项目后,从左侧菜单转到“数据库”,您将在其中找到firebase数据库的URL(如上所述)。

答案 3 :(得分:0)

试试这个:

cannot access ActivityCompatApi23
public abstract class NetworkActivity<T extends java.lang.Object> extends android.support.v4.app.FragmentActivity implements courier.Hub, courier.Receiver<eu.myapp.registration.network.HttpResult<T>> {
                ^
  class file for android.support.v4.app.ActivityCompatApi23 not found

并将模型类dataBinding { enabled = true } 变量公开。 for more help check this link

相关问题