gpt4 book ai didi

java - Firebase简单聊天客户端无法检索值

转载 作者:行者123 更新时间:2023-12-01 12:23:35 25 4
gpt4 key购买 nike

我正在尝试构建基于 firebase 的最简单的 android 聊天应用程序,我从他们的 android 聊天示例中获取了用于聊天的 firebase 列表适配器。并使用了我自己的模型类,但该死的东西不起作用。这是类和日志:

消息模型类:

导入java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;
import com.shaded.fasterxml.jackson.annotation.JsonIgnore;


public class Message {
int messageId;
String messageContent;

// int chatID;
// int senderID;
// int recieverID;
// Date timeStamp;
// String messageType;

/**
* Message constructor to create message object
*
* @param messageID
* @param messageContent
* @param chatID
* {@link Chat}
* @param senderID
* {@link User}
* @param recieverID
* {@link User}
* @param messageType
*/
// public Message(int messageID, String messageContent, int chatID,
// int senderID, int recieverID, MessageType messageType) {
// this.messageID = messageID;
// this.messageContent = messageContent;
// this.chatID = chatID;
// this.senderID = senderID;
// this.recieverID = recieverID;
// this.messageType = messageType;
// }

@JsonIgnore
public Message(int messageId, String messageContent) {
// this.messageContent = messageContent;
this.messageId = messageId;
this.messageContent = messageContent;
}

/**
* Message constructor to create message object This will be used when the
* user send the message and still no status for this message
*
* @param messageContent
* @param chatID
* {@link Chat}
* @param senderID
* {@link User}
* @param recieverID
* {@link User}
* @param messageType
*/
// public Message(String messageContent, int chatID, int senderID,
// int recieverID, MessageType messageType) {
// this.messageContent = messageContent;
// this.chatID = chatID;
// this.senderID = senderID;
// this.recieverID = recieverID;
// this.messageType = messageType;
// }

/**
* Message constructor to create message object from JSON response This
* constructor will be used to create an object from web service JSON
* response
*
* @param message
* @throws JSONException
*/
// public Message(JSONObject message) throws JSONException {
// messageID = message.getInt("messageID");
// messageContent = message.getString("messageContent");
// chatID = message.getInt("chatID");
// senderID = message.getInt("senderID");
// recieverID = message.getInt("recieverID");
// messageType = MessageType.values()[message.getInt("messageType")];
// }

/**
*
* @return messageID
*/
public int getMessageId() {
return messageId;
}

/**
*
* @param messageID
*/
public void setMessageId(int messageId) {
this.messageId = messageId;
}

/**
*
* @return messageContent
*/
public String getMessageContent() {
return messageContent;
}

/**
*
* @param messageContent
*/
public void setMessageContent(String messageContent) {
this.messageContent = messageContent;
}

/**
*
* @return chatID
*/
// public int getChatID() {
// return chatID;
// }
//
// /**
// *
// * @param chatID
// */
// public void setChatID(int chatID) {
// this.chatID = chatID;
// }

/**
*
* @return senderId
*/
// public int getSenderId() {
// return senderId;
// }
//
// //
// // /**
// // *
// // * @param senderID
// // */
// public void setSenderId(int senderId) {
// this.senderId = senderId;
// }

/**
*
* @return recieverID
*/
// public int getRecieverID() {
// return recieverID;
// }
//
// /**
// *
// * @param recieverID
// */
// public void setRecieverID(int recieverID) {
// this.recieverID = recieverID;
// }

/**
*
* @return timeStamp
*/
// public Date getTimeStamp() {
// return timeStamp;
// }
//
// /**
// *
// * @param timeStamp
// */
// public void setTimeStamp(Date timeStamp) {
// this.timeStamp = timeStamp;
// }

/**
*
* @return messageType
*/
// public MessageType getMessageType() {
// return messageType;
// }
//
// /**
// *
// * @param messageType
// */
// public void setMessageType(MessageType messageType) {
// this.messageType = messageType;
// }

}

这是适配器类:

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* User: greg
* Date: 6/21/13
* Time: 1:47 PM
*/

/**
* This class is a generic way of backing an Android ListView with a Firebase location.
* It handles all of the child events at the given Firebase location. It marshals received data into the given
* class type. Extend this class and provide an implementation of <code>populateView</code>, which will be given an
* instance of your list item layout and an instance your class that holds your data. Simply populate the view however
* you like and this class will handle updating the list as the data changes.
* @param <Message> The class type to use as a model for the data contained in the children of the given Firebase location
*/
public abstract class FirebaseListAdapter<Message> extends BaseAdapter {

private Query ref;
private Class<Message> modelClass;
private int layout;
private LayoutInflater inflater;
private List<Message> models;
private Map<String, Message> modelNames;
private ChildEventListener listener;


/**
* @param ref The Firebase location to watch for data changes. Can also be a slice of a location, using some
* combination of <code>limit()</code>, <code>startAt()</code>, and <code>endAt()</code>,
* @param modelClass Firebase will marshall the data at a location into an instance of a class that you provide
* @param layout This is the layout used to represent a single list item. You will be responsible for populating an
* instance of the corresponding view with the data from an instance of modelClass.
* @param activity The activity containing the ListView
*/
public FirebaseListAdapter(Query ref, Class<Message> modelClass, int layout, Activity activity) {
this.ref = ref;
this.modelClass = modelClass;
this.layout = layout;
inflater = activity.getLayoutInflater();
models = new ArrayList<Message>();
modelNames = new HashMap<String, Message>();
// Look for all child events. We will then map them to our own internal ArrayList, which backs ListView
listener = this.ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {

Log.v("KK", "starded listener");
Message model = dataSnapshot.getValue(FirebaseListAdapter.this.modelClass);
modelNames.put(dataSnapshot.getName(), model);
Log.v("KK", "faga3");

// Insert into the correct location, based on previousChildName
if (previousChildName == null) {
models.add(0, model);
} else {
Message previousModel = modelNames.get(previousChildName);
int previousIndex = models.indexOf(previousModel);
int nextIndex = previousIndex + 1;
if (nextIndex == models.size()) {
models.add(model);
} else {
models.add(nextIndex, model);
}
}

notifyDataSetChanged();
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

// One of the models changed. Replace it in our list and name mapping
String modelName = dataSnapshot.getName();
Message oldModel = modelNames.get(modelName);
Message newModel = dataSnapshot.getValue(FirebaseListAdapter.this.modelClass);
int index = models.indexOf(oldModel);

models.set(index, newModel);
modelNames.put(modelName, newModel);

notifyDataSetChanged();
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

// A model was removed from the list. Remove it from our list and the name mapping
String modelName = dataSnapshot.getName();
Message oldModel = modelNames.get(modelName);
models.remove(oldModel);
modelNames.remove(modelName);
notifyDataSetChanged();
}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {

// A model changed position in the list. Update our list accordingly
String modelName = dataSnapshot.getName();
Message oldModel = modelNames.get(modelName);
Message newModel = dataSnapshot.getValue(FirebaseListAdapter.this.modelClass);
int index = models.indexOf(oldModel);
models.remove(index);
if (previousChildName == null) {
models.add(0, newModel);
} else {
Message previousModel = modelNames.get(previousChildName);
int previousIndex = models.indexOf(previousModel);
int nextIndex = previousIndex + 1;
if (nextIndex == models.size()) {
models.add(newModel);
} else {
models.add(nextIndex, newModel);
}
}
notifyDataSetChanged();
}

@Override
public void onCancelled(FirebaseError arg0) {
// TODO Auto-generated method stub

}
});
}

public void cleanup() {
// We're being destroyed, let go of our listener and forget about all of the models
ref.removeEventListener(listener);
models.clear();
modelNames.clear();
}

@Override
public int getCount() {
return models.size();
}

@Override
public Object getItem(int i) {
return models.get(i);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(layout, viewGroup, false);
}

Message model = models.get(i);
// Call out to subclass to marshall this model into the provided view
populateView(view, model);
return view;
}

/**
* Each time the data at the given Firebase location changes, this method will be called for each item that needs
* to be displayed. The arguments correspond to the layout and modelClass given to the constructor of this class.
*
* Your implementation should populate the view using the data contained in the model.
* @param v The view to populate
* @param model The object containing the data used to populate the view
*/
protected abstract void populateView(View v, Message model);

}

最后是日志猫

    10-22 11:30:15.276: V/KK(14856): starded listener
10-22 11:30:15.296: D/AndroidRuntime(14856): Shutting down VM
10-22 11:30:15.296: W/dalvikvm(14856): threadid=1: thread exiting with uncaught exception (group=0x415b0ba8)
10-22 11:30:15.306: E/AndroidRuntime(14856): FATAL EXCEPTION: main
10-22 11:30:15.306: E/AndroidRuntime(14856): Process: ********, PID: 14856
10-22 11:30:15.306: E/AndroidRuntime(14856): com.firebase.client.FirebaseException: Failed to bounce to type
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:186)
10-22 11:30:15.306: E/AndroidRuntime(14856): at ******$1.onChildAdded(FirebaseListAdapter.java:66)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.firebase.client.core.ChildListenerContainer$1.run(ChildListenerContainer.java:49)
10-22 11:30:15.306: E/AndroidRuntime(14856): at android.os.Handler.handleCallback(Handler.java:733)
10-22 11:30:15.306: E/AndroidRuntime(14856): at android.os.Handler.dispatchMessage(Handler.java:95)
10-22 11:30:15.306: E/AndroidRuntime(14856): at android.os.Looper.loop(Looper.java:136)
10-22 11:30:15.306: E/AndroidRuntime(14856): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-22 11:30:15.306: E/AndroidRuntime(14856): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 11:30:15.306: E/AndroidRuntime(14856): at java.lang.reflect.Method.invoke(Method.java:515)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-22 11:30:15.306: E/AndroidRuntime(14856): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
10-22 11:30:15.306: E/AndroidRuntime(14856): at dalvik.system.NativeStart.main(Native Method)
10-22 11:30:15.306: E/AndroidRuntime(14856): Caused by: com.shaded.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.annabsoft.androidapp.tilochatlibrary.models.Message] from String value; no single-String constructor/factory method
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.shaded.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.shaded.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1056)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:136)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
10-22 11:30:15.306: E/AndroidRuntime(14856): at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:182)
10-22 11:30:15.306: E/AndroidRuntime(14856): ... 12 more
10-22 11:35:15.466: I/Process(14856): Sending signal. PID: 14856 SIG: 9

最佳答案

只是为了形式化答案,以便对其搜索此内容的其他人有用:

使用 DataSnapshot.getValue(YourModel.Class) 您可以获得一个已解析的对象,其结构与 Firebase 中定义的结构相同。只需确保 YourModel.class 具有该结构即可。

将返回的对象序列化为 POJO 时,它必须包含与 Firebase 对象完全相同的属性。

关于java - Firebase简单聊天客户端无法检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26503404/

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