【发布时间】:2016-03-27 10:22:02
【问题描述】:
我使用 Firebase 作为我的应用的后端。但我无法将 Firebase 中的 PolylineOptions 序列化回 PolylineOptions 类型。如果我尝试使用 dataSnapshot.getValue(PolylineOptions.class); 将其转换回来,我会收到以下错误消息:
com.firebase.client.FirebaseException: Failed to bounce to type
....
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "width" (class com.google.android.gms.maps.model.PolylineOptions), not marked as ignorable (one known property: "points"])
....
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
我该如何解决这个问题?
这是演示该问题的代码:
import android.location.Location;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.firebase.client.Query;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.maps.model.LatLng;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
public class MainActivity extends AppCompatActivity{
PolylineOptions mPolylineOptions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPolylineOptions = new PolylineOptions();
Location targetLocation = new Location("name");
targetLocation.setLatitude(1.0d);
targetLocation.setLongitude(2.0d);
mPolylineOptions.add(new LatLng(targetLocation.getLatitude(), targetLocation.getLongitude()));
mPolylineOptions.color(ContextCompat.getColor(this, R.color.colorPrimary));
Firebase.setAndroidContext(this);
Firebase ref = new Firebase("https://xxxxxxx");
Firebase testRef = ref.child("test");
final Query queryRef = testRef.orderByKey();
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Test", "data change");
PolylineOptions activity1 = dataSnapshot.getValue(PolylineOptions.class);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
testRef.setValue(mPolylineOptions);
}
}
这是来自 Firebase 的 JSON:
这是来自 Firebase 的 JSON:
{
"test" : {
"clickable" : false,
"color" : -12627531,
"geodesic" : false,
"points" : [ {
"latitude" : 50.68468468468468,
"longitude" : 8.190167190445726
} ],
"visible" : true,
"width" : 10.0,
"zindex" : 0.0
}
}
【问题讨论】:
-
正如错误消息所说:你的 Java 对象有一个
field而你的 JSON 有一个points。在没有看到更多有用信息的情况下,我们在这里无话可说。将 JSON 的 sn-p 添加为文本(无屏幕截图),您可以使用 Firebase 仪表板中的“导出”按钮轻松获取它然后还添加用于从数据库中读取该 JSON 的代码。见MCVE。 -
已将完整的代码示例粘贴到顶帖。我还发布了 JSON 表单 Firebase...
-
请注意,MCVE 中的 M 代表 minimal 。我怀疑位置处理本身与异常有关。因此,如果您将其从问题中删除(同时仍然保持代码正常工作并重现问题),您就可以节省我们理解它的作用的努力。您越容易让我们帮助您,就越有可能有人会提供帮助。
-
您有一个类
PolylineOptions用于读取 JSON。您显然在该类或 JSON 中缺少一些属性。但再说一遍:没有看到课程,就不可能说更多(尽管乍一看,points数组看起来像一个问题世界)。 -
再次添加了更短的代码。你是指哪个班?我以为 PolylineOptions 是一个模型类,因为包名是 com.google.android.gms.maps.model。有了模型类,我可以序列化和反序列化!?
标签: android serialization firebase