【问题标题】:Firebase Serialize PolylineOptionsFirebase 序列化折线选项
【发布时间】: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


【解决方案1】:

读取 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;
}

我不一定将这些称为最佳实践,因为它们更像 C structs 而不是类。为了让它们稍微干净一点:

  • 将所有字段设为private,而不是public
  • 为每个字段添加一个getter方法(所以getLatitude()和getLongitude()为class LatLon)

【讨论】:

    【解决方案2】:

    我已经编写了一小段代码,这里是我的功能模型:

    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 对象,因此我也不需要为该类型编写模型。如何说服我的应用也采用我的模型?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 2023-04-05
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      相关资源
      最近更新 更多