java在使用Firebase的Android上检索数据时出现问题
我正在学习如何在Android中设置Firebase数据。我可以写入数据库,但我无法读取它并将其显示回我的应用程序。我的应用程序非常简单(目前只有一个主屏幕),我不在乎数据如何显示在页面上,因为我只是在学习——我现在正在尝试在文本视图中显示它
以下是我的主要活动:
package com.example.ispotrachel.myapplication;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.TextView;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
Firebase ref = new Firebase("https://blistering-torch-9015.firebaseio.com");
class User {
private int birthYear;
private String fullName;
public User() {
}
public User(String fullName, int birthYear) {
this.fullName = fullName;
this.birthYear = birthYear;
}
public long getBirthYear() {
return birthYear;
}
public String getFullName() {
return fullName;
}
}
User user1 = new User("Name 1", 1982);
User user2 = new User("Name 2", 1972);
Firebase usersRef = ref.child("users");
Map<String, User> users = new HashMap<String, User>();
users.put("user1", user1);
users.put("user 2", user2);
usersRef.setValue(users);
Firebase postRef = ref.child("posts");
Map<String, String> post1 = new HashMap<String, String>();
post1.put("author", "user1");
post1.put("title", "Announcing COBOL, a New Programming Language");
postRef.push().setValue(post1);
Map<String, String> post2 = new HashMap<String, String>();
post2.put("author", "user2");
post2.put("title", "The Turing Machine");
postRef.push().setValue(post2);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot arg0) {
//System.out.println(arg0.getValue());
TextView textViewSample = (TextView) findViewById(R.id.sampleTextView);
textViewSample.setText(arg0.getValue(String.class));
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("the read failed");
}
});
}}
这是我的主要活动。xml:
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent" 安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
安卓:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
安卓:id="@+id/sampleTextView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/hello_world"/>
</LinearLayout>
当我运行应用程序时,它崩溃了。以下是logcat错误消息:
12-27 12:15:02.678 1895-1895/com.example.ispotrachel.myapplication I/art﹕ Not late enabling -Xcheck:jni (already on)
12-27 12:15:02.946 1895-1907/com.example.ispotrachel.myapplication I/art﹕ Background sticky concurrent mark sweep GC freed 3867(286KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 1009KB/1135KB, paused 2.595ms total 117.910ms
12-27 12:15:03.317 1895-1926/com.example.ispotrachel.myapplication D/OpenGLRenderer﹕ Render dirty regions requested: true
12-27 12:15:03.320 1895-1895/com.example.ispotrachel.myapplication D/﹕ HostConnection::get() New Host Connection established 0xa6667e80, tid 1895
12-27 12:15:03.327 1895-1895/com.example.ispotrachel.myapplication D/Atlas﹕ Validating map...
12-27 12:15:03.391 1895-1926/com.example.ispotrachel.myapplication D/﹕ HostConnection::get() New Host Connection established 0xa66ef3b0, tid 1926
12-27 12:15:03.405 1895-1926/com.example.ispotrachel.myapplication I/OpenGLRenderer﹕ Initialized EGL, version 1.4
12-27 12:15:03.422 1895-1926/com.example.ispotrachel.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0
12-27 12:15:03.439 1895-1926/com.example.ispotrachel.myapplication W/EGL_emulation﹕ eglSurfaceAttrib not implemented
12-27 12:15:03.439 1895-1926/com.example.ispotrachel.myapplication W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa66b7880, error=EGL_SUCCESS
12-27 12:15:07.474 1895-1926/com.example.ispotrachel.myapplication W/EGL_emulation﹕ eglSurfaceAttrib not implemented
12-27 12:15:07.474 1895-1926/com.example.ispotrachel.myapplication W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa66b7880, error=EGL_SUCCESS
12-27 12:15:13.993 1895-1895/com.example.ispotrachel.myapplication D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
12-27 12:15:13.993 1895-1895/com.example.ispotrachel.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ispotrachel.myapplication, PID: 1895 com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:196)
at com.example.ispotrachel.myapplication.MainActivity$1.onDataChange(MainActivity.java:79)
at com.firebase.client.core.ValueEventRegistration$1.run(ValueEventRegistration.java:48)
at 安卓.os.Handler.handleCallback(Handler.java:739)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:135)
at 安卓.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.StringReader@c8f7297; line: 1, column: 1]
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:46)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:192)
at com.example.ispotrachel.myapplication.MainActivity$1.onDataChange(MainActivity.java:79)
at com.firebase.client.core.ValueEventRegistration$1.run(ValueEventRegistration.java:48)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:135)
at 安卓.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:694)device not found
# 1 楼答案
我也有类似的问题
以下是我如何从firebase检索数据的代码:
直到我没有使用push()添加新项目,它才开始工作。我在firebase控制台中的数据看起来像here
例外情况文本:
我用了这个模型:
我发现,如果数据列表中包含键,例如“-kldskjdkjdjdjdjdjdjdjdjdjdjjsjk”firebase,则会将孔列表识别为“HashMap”,否则识别为“ArrayList”。我将我的模型改为这种结构:
并在聊天信息的其余部分添加了类似“-kldskjdkjdkjdksjk”的键。我的代码现在正在运行。但我认为这并不明显
# 2 楼答案
tl;dr
看起来您正在尝试将JSON对象加载到字符串中
错误消息
Failed to bounce to type
表示Firebase客户端返回了一些数据,但无法将其转换为您指定的类型有关问题的详细信息
您正在将
ValueEventListener
添加到Firebase的根目录:在该值事件侦听器中,您告诉Firebase将整个Firebase作为
String
加载:如果你有一个非常简单的Firebase,它只包含一个字符串,这就可以了。然而,从代码的其余部分来看,Firebase中似乎不止一个
String
:)如何修复它 你有几个选择:
将事件侦听器连接到树的更下方。您可以通过字符串获取用户的全名:
或者,您可以获得一个完整的用户,但将其解析为
User
,而不是String
: