Android Firebase序列化多段线选项
我使用Firebase作为我的应用程序的后端。但我无法将Firebase中的PolylineOptions序列化回PolylineOptions类型。如果我尝试使用dataSnapshot.getValue(PolylineOptions.class)将其转换回来,我收到以下错误消息:Android Firebase序列化多段线选项,android,serialization,firebase,Android,Serialization,Firebase,我使用Firebase作为我的应用程序的后端。但我无法将Firebase中的PolylineOptions序列化回PolylineOptions类型。如果我尝试使用dataSnapshot.getValue(PolylineOptions.class)将其转换回来,我收到以下错误消息: com.firebase.client.FirebaseException: Failed to bounce to type .... Caused by: com.fasterxml.jackson.da
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:
{
"test" : {
"clickable" : false,
"color" : -12627531,
"geodesic" : false,
"points" : [ {
"latitude" : 50.68468468468468,
"longitude" : 8.190167190445726
} ],
"visible" : true,
"width" : 10.0,
"zindex" : 0.0
}
}
以下是来自Firebase的JSON:
{
"test" : {
"clickable" : false,
"color" : -12627531,
"geodesic" : false,
"points" : [ {
"latitude" : 50.68468468468468,
"longitude" : 8.190167190445726
} ],
"visible" : true,
"width" : 10.0,
"zindex" : 0.0
}
}
读取JSON所需的最少类为:
public static class LatLon {
public double latitude;
public double longitude;
}
public static class Test {
public boolean clickable;
public long color;
public boolean geodesic;
public LatLon[] points;
public boolean visible;
public double width;
public double zindex;
}
我不一定把它们称为最佳实践,因为它们更像Cstruct
s,而不是类。要使其稍微清洁,请执行以下操作:
- 如果
公共
- 为每个字段添加一个getter方法(因此
和getLatitude()
用于getLongitude()
)类LatLon
public class PolylineOptions {
public boolean clickable;
public long color;
public boolean geodesic;
public LatLng[] points = new LatLng[1];;
public boolean visible;
public double width;
public double zindex;
public PolylineOptions(){
}
public void add(LatLng point){
points[0] = point;
}
}
public class LatLng {
public double latitude;
public double longitude;
public LatLng(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public LatLng(){
}
}
但我经常收到这样的信息:
Error:(32, 32) error: incompatible types: app.firebasetest.PolylineOptions cannot be converted to com.google.android.gms.maps.model.PolylineOptions
在线上,例如:
GoogleMaps mGoogleMap.addPolyline(mPolylineOptions);
我不需要Firebase中的GoogleMaps对象,所以我也不需要为这种类型编写模型。我怎样才能说服我的应用程序也采用我的模型呢?正如错误消息所说:Java对象有一个
字段
,而JSON有一个点
。如果看不到更多有用的信息,我们在这里就没什么可说的了。添加一段JSON作为文本(无屏幕截图),您可以通过使用Firebase仪表板中的导出按钮轻松获取该文本,然后还可以添加用于从数据库读取JSON的代码。请参阅。已将完整的代码示例粘贴到顶部帖子。我还发布了JSON表单Firebase…请注意,MCVE中的M代表最小值。我怀疑位置处理本身与异常相关。因此,如果您将其从问题中删除(同时仍然保持代码工作并再现问题),那么您就省去了我们理解它的功能的努力。我们越容易帮助您,就越有可能有人会帮助您。您有一个类PolylineOptions
,用于读取JSON。显然,该类或JSON中缺少一些属性。但是再次强调:如果不看这个类,就不可能说得更多(虽然points
数组乍一看像是一个问题的世界)。再次添加了一个较短的代码。你指的是哪一类?我认为PolylineOptions是一个模型类,因为包名是com.google.android.gms.maps.model。使用模型类,我可以序列化和反序列化!?