2016-03-27 8 views
-1

私は自分のアプリケーションのバックエンドとしてFirebaseを使用します。しかし、FirebaseからPolylineOptionsをPolylineOptions型にシリアル化することはできません。もし私がそれを元に戻そうとするとdataSnapshot.getValue(PolylineOptions.class);は、私は、次のエラーメッセージがありますFirebaseはPolylineOptionsをシリアル化します

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

エラーメッセージによると、JSONに 'points'がある間、あなたのJavaオブジェクトには' field'があります。より有益な情報を見ずにここで言うことは何もありません。 Firebaseダッシュボードの[エクスポート]ボタンを使用して簡単に取得できるテキスト(スクリーンショットなし)としてスニペットを追加し、そのJSONをデータベースから読み取るために使用するコードも追加します。 [MCVE](http://stackoverflow.com/help/mcve)を参照してください。 –

+0

トップポストに完全なコード例をペーストしてください。また、JSONフォームFirebaseを投稿しました。 –

+0

MCVEのMは** minimal **の略です。私は、その場所の処理自体が例外に関連しているのではないかと疑う。だから、あなたが質問からそれを取り除いても(コードを動作させて問題を再現している間)、あなたはそれが何をするのかを理解する努力を惜しまない。私たちがあなたに手伝ってもらいやすいほど、誰かが助けてくれる可能性が高くなります。 –

答えて

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に近いので、必ずしもベストプラクティスとは言いません。彼らは少しきれいにする:public

  • は、フィールドごとに(class LatLonためのとてもgetLatitude()getLongitude())のgetterメソッドを追加した場合

    • ではなく、すべてのフィールドprivateを作る
  • 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(){ 
    
        } 
    } 
    

    しかし、私は非常に頻繁にarror messaそのようなGES:のような行に

    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でGoogleマップのオブジェクトを必要といけないので、私はあまりにもそのタイプのモデルを記述する必要がいけません。どのように私のモデルを引き受けるために私のアプリを説得することができますか?

    関連する問題