com.google.firebase.database.DatabaseException:不支持字符,请输入字符串

时间:2017-09-17 16:40:18

标签: java android firebase firebase-realtime-database

我是Firebase的新手。我正在尝试保存从Android发送到实时数据库的POJO。

我有一个像这样的POJO:

@IgnoreExtraProperties
public class Member implements Parcelable{

public String name;

public Member() {}

public Member(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

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

/**=================== */
/** Parcelable section */
/**=================== */

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public Member createFromParcel(Parcel in) {
        return new Member(in);
    }

    public Member[] newArray(int size) {
        return new Member[size];
    }
};

public Member(Parcel in){
    this.name = in.readString();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.name);
}
}

这样的活动:

package com.example.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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

import com.example.R;
import com.example.model.Member;

public class TestActivity extends AppCompatActivity {

DatabaseReference myRef;

Button button;
TextView text;

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

    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    myRef = FirebaseDatabase.getInstance().getReference();

    text = (TextView) findViewById(R.id.text_test);
    button = (Button) findViewById(R.id.button_test);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendDataToFirebase();
        }
    });
}

private void sendDataToFirebase() {
    String key = myRef.push().getKey();
    Member member = new Member("John");
    myRef.child("Member").child("john").child(key).setValue(member);
}
}

单击按钮后出现此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 16332
com.google.firebase.database.DatabaseException: Characters are not supported, please strings
    at com.google.android.gms.internal.zg.zzao(Unknown Source)
    at com.google.android.gms.internal.zg.zzar(Unknown Source)
    at com.google.android.gms.internal.zh.zzas(Unknown Source)
    at com.google.android.gms.internal.zg.zzao(Unknown Source)
    at com.google.android.gms.internal.zg.zzan(Unknown Source)
    at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
    at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
    at com.example.view.TestActivity.sendDataToFirebase(TestActivity.java:43)
    at com.example.view.TestActivity.access$000(TestActivity.java:15)
    at com.example.view.TestActivity$1.onClick(TestActivity.java:35)
    at android.view.View.performClick(View.java:5714)
    at android.widget.TextView.performClick(TextView.java:10926)
    at android.view.View$PerformClick.run(View.java:22594)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7325)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我无法找出我的代码有什么问题。我已经关注了Firebase official website和其他教程的教程。我小心翼翼地跟着他们,我已经多次尝试过了。我谷歌搜索了几个小时,但没有希望。

希望能从这个社区获得一些解决方案或建议。 非常感谢:))

1 个答案:

答案 0 :(得分:0)

当我在构造函数中初始化对象元素而不传递它的值时,我遇到了同样的问题。喜欢:

public class ChatMessage {
private String messageText;
private String messageUser;
private String messageTime;
private String photoURL;

dateFormat = new SimpleDateFormat("hh:mm aa");

public ChatMessage() {
}

public ChatMessage(String messageText, String messageUser, String photoURL) {
    this.messageText = messageText;
    this.messageUser = messageUser;
    this.photoURL = photoURL;
    **this.messageTime = dateFormat.format(new Date()).toString();**
}


public String getMessageText() {
    return messageText;
}

public void setMessageText(String messageText) {
    this.messageText = messageText;
}

public String getMessageUser() {
    return messageUser;
}

public void setMessageUser(String messageUser) {
    this.messageUser = messageUser;
}

public String getMessageTime() {
    return messageTime;
}

public void setMessageTime(String messageTime) {
    this.messageTime = messageTime;
}

public String getPhotoURL() {
    return photoURL;
}

public void setPhotoURL(String photoURL) {
    this.photoURL = photoURL;
}

}

然后我改为:

    public class ChatMessage {
private String messageText;
private String messageUser;
private String messageTime;
private String photoURL;


public ChatMessage() {
}

public ChatMessage(String messageText, String messageUser, String photoURL, String messageTime) {
    this.messageText = messageText;
    this.messageUser = messageUser;
    this.photoURL = photoURL;
    this.messageTime = messageTime;
}


public String getMessageText() {
    return messageText;
}

public void setMessageText(String messageText) {
    this.messageText = messageText;
}

public String getMessageUser() {
    return messageUser;
}

public void setMessageUser(String messageUser) {
    this.messageUser = messageUser;
}

public String getMessageTime() {
    return messageTime;
}

public void setMessageTime(String messageTime) {
    this.messageTime = messageTime;
}

public String getPhotoURL() {
    return photoURL;
}

public void setPhotoURL(String photoURL) {
    this.photoURL = photoURL;
}
}

使用MainActivity中的以下行在FireBaseDataBase中推送POJO:

     ChatMessage msg = new 
     ChatMessage(s,mUserName,null,dateFormat.format(new Date()).toString());
     mMessagesDatabaseReference.push().setValue(msg);

这对我现在使用firebase数据库。希望这对你也有帮助。