gpt4 book ai didi

java - Android - Firebase - 无法将 java.util.HashMap 类型的值转换为字符串

转载 作者:行者123 更新时间:2023-11-30 10:24:33 25 4
gpt4 key购买 nike

瞄准

在群聊节点内保存数据

当前数据库树

enter image description here

在群聊节点中,用户的消息将被发送并存储在用户选择的群聊中。

如数据库树所示,用户发送了一条消息“Hello”,他的姓名从用户节点中检索

所需的数据库树

enter image description here

---消息ID---是自动生成的

---用户 ID--- 从用户节点中检索

问题

允许用户发送消息Only Worked Once 的功能,这是第一次 时间。以下几次部署应用程序时,它在用户登录时崩溃。

错误 - Logcat

com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String

ChatActivity 类 - 发送聊天消息功能

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

jGroupChatName = getIntent().getExtras().get("groupChatName").toString();
jUserName = getIntent().getExtras().get("groupUserName").toString();
jUserNeighbourhood = getIntent().getExtras().get("groupUserHome").toString();

jChatToolbar = (Toolbar) findViewById(R.id.allUSersToolBar);

jFirebaseCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

/*UserID Start - I was trying to retrieve the current user ID and add it to the Group Chat node,
under the selected Group Chat */
assert jFirebaseCurrentUser != null;
final String currentUserID = jFirebaseCurrentUser.getUid();
jChatRoot = FirebaseDatabase.getInstance().getReference().child("Group Chats").child(jGroupChatName).child(currentUserID);
/*UserID End*/

jChatMessageText = (EditText) findViewById(R.id.chatMessageText);
jChatSendTextBtn = (ImageButton) findViewById(R.id.chatSendTextBtn);

jChatSendTextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Map<String, Object> groupMap = new HashMap<String, Object>();
jGroupKey = jChatRoot.push().getKey();
jChatRoot.updateChildren(groupMap);

// Based on the UserID code at the top, the "currentUserID" is added as shown below
// My intentions are to have the UserID saved under the name of the Group Chat
// jGroupKey = (Name of selected Group Chat)

DatabaseReference groupMessageRoot = jChatRoot.child(jGroupKey).child(currentUserID);
Map<String, Object> groupMap2 = new HashMap<String, Object>();
groupMap2.put("name", jUserName);
groupMap2.put("msg", jChatMessageText.getText().toString());

groupMessageRoot.updateChildren(groupMap2);
}
});
}

相关问题

Android - Firebase - Send Users to Chat Room

future 的实现

在未来,我打算通过编写代码允许用户发送图片和视频记录,允许用户访问他们的手机相机或画廊,并将图片和视频数据发送到群聊节点,在选定的团队名字。我也想在发送后检索数据,但是当然,如​​果我无法解决它们,我会把这些留在以后的问题中。

最佳答案

不需要对所需数据库进行分支。您正在使用用户的 UID 创建不必要的级别。相反,将这一层向下合并,即合并 uid、消息和名称。然后您可以使用 someRef.orderByKey("uid").equalTo("<user_uid>")如果您需要该路径中特定用户的记录,例如聊天消息。

因此,创建一个名为 ChatMessage 的类:

public class ChatMessage {

private String uid;
private String name;
private String message;
private Long creationTime;

public ChatMessage() {
// Default constructor required for calls to DataSnapshot.getValue(ChatMessage.class)
}

public ChatMessage(String uid, String name, String message) {
this.uid = uid;
this.name = name;
this.message = message;
}

public String getUid() {
return uid;
}

public void setUid(String uid) {
this.uid = uid;
}

public String getName() {
return name;
}

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

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public java.util.Map<String, String> getCreationTime() {
return ServerValue.TIMESTAMP;
}

@Exclude
public Long getCreationTimeLong() {
return creationTime;
}

public void setCreationTime(Long creationTime) {
this.creationTime = creationTime;
}
}

然后按如下方式更新您的代码:

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

chatGroupName = getIntent().getStringExtra(CHAT_GROUP_NAME);
userKey = getIntent().getStringExtra(USER_KEY);
user = getIntent().getParcelableExtra(USER);

jChatToolbar = (Toolbar) findViewById(R.id.allUSersToolBar);
jChatMessageText = (EditText) findViewById(R.id.chatMessageText);
jChatSendTextBtn = (ImageButton) findViewById(R.id.chatSendTextBtn);

jChatSendTextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
}

private void sendMessage() {
String userMessage = jChatMessageText.getText().toString();
chatGroupRef = FirebaseDatabase.getInstance().getReference().child("Group Chats").child(chatGroupName);

if (!TextUtils.isEmpty(userMessage)) {
String newChatMessageKey = chatGroupRef.push().getKey();
ChatMessage newChatMessage = new ChatMessage(userKey, user.getName(), userMessage);
chatGroupRef.child(newChatMessageKey).setValue(newChatMessage);
}
}

关于java - Android - Firebase - 无法将 java.util.HashMap 类型的值转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46471541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com