我使用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
}
}
答案 0 :(得分: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 struct
而不是类。为了使它们更清洁:
private
public
getLatitude()
getLongitude()
和class LatLon
答案 1 :(得分:0)
我编写了一个liitle位,这是我的功能模型:
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对象,因此我也不需要为该类型编写模型。我如何说服我的应用程序也承担我的模型?