【问题标题】:JsonMappingException: No suitable constructor found for typeJsonMappingException:找不到适合类型的构造函数
【发布时间】:2016-05-03 06:17:47
【问题描述】:

我正在使用 FirebaseUI 填充 RecyclerView

public class ViewRequestsFragment extends Fragment {
    private Firebase myUserFire,myListFire;
    protected RecyclerView.LayoutManager mLayoutManager;
    private FirebaseRecyclerAdapter<Request,RecyclerViewHolder> recyclerAdapter;
    public ViewRequestsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String firbaseUrl= getArguments().getString(Constants.FIREBASE);
        myUserFire = new Firebase(firbaseUrl);
        myListFire = new Firebase(Constants.FIREBASE_REQUESTS);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View rootView= inflater.inflate(R.layout.fragment_view_requests, container, false);
        final RecyclerView recyclerView= (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
        FragmentActivity fragmentActivity=getActivity();
        mLayoutManager = new LinearLayoutManager(fragmentActivity);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(mLayoutManager);

        recyclerAdapter = new FirebaseRecyclerAdapter<Request, RecyclerViewHolder>(Request.class,R.layout.reques_list,RecyclerViewHolder.class, myListFire) {
            @Override
            protected void populateViewHolder(RecyclerViewHolder recyclerViewHolder, Request request, int i) {
                recyclerViewHolder.tv1.setText(request.getRiderId());
                recyclerViewHolder.tv2.setText(request.getDriverId());
            }


        };
        recyclerView.setAdapter(recyclerAdapter);
        return  rootView;
    }

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
        TextView tv1, tv2;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            tv1 = (TextView) itemView.findViewById(R.id.list_title);
            tv2 = (TextView) itemView.findViewById(R.id.list_desc);
        }
    }

}

我收到以下错误

原因:com.fasterxml.jackson.databind.JsonMappingException:没有找到适合类型[简单类型,类 com.google.android.gms.maps.model.LatLng] 的构造函数:无法从 JSON 对象实例化(需要添加/启用类型信息?)

这是我的 POJO 类

public  class Request {       
    private String driverId;
    private LatLng latLng;

    public Request(){}

    public Request(String driverId) {           
        this.driverId = driverId;
    }   

    public Request(String driverId, LatLng latLng) {           
        this.driverId = driverId;
        this.latLng = latLng;
    } 
    public String getDriverId() {
        return driverId;
    }

    public void setDriverId(String driverId) {
        this.driverId = driverId;
    }

    public LatLng getLatLng() {
        return latLng;
    }

    public void setLatLng(LatLng latLng) {
        this.latLng = latLng;
    }
}

这是我要反序列化的 JSON:

 {
  "driverId" : "null",
  "latLng" : {
    "latitude" : 38.421998333333335,
    "longitude" : -121.08400000000002
  }
}

我应该如何设计我的响应类以使用 "latLng",任何帮助将不胜感激。

【问题讨论】:

  • 您很可能正在尝试读/写Google Maps LatLng object。这是行不通的,因为该类具有 Firebase 无法读取/写入的属性。您需要创建自己的类来模拟纬度/经度,例如@yozzy 的答案中的那个。
  • @FrankvanPuffelen 非常感谢,我正在尝试读取 Google Maps LatLng 对象。

标签: android firebase firebase-realtime-database


【解决方案1】:

您必须构建两个不同的模型:Request 和 LatLon。

请求:

public  class Request implements Serializable {       
    private String driverId;
    private LatLng latLng;

    public Request(){}

    public Request(String driverId) {           
        this.driverId = driverId;
    }   

    public Request(String driverId, LatLng latLng) {           
        this.driverId = driverId;
        this.latLng = latLng;
    } 
    public String getDriverId() {
        return driverId;
    }

    public void setDriverId(String driverId) {
        this.driverId = driverId;
    }

    public LatLng getLatLng() {
        return latLng;
    }

    public void setLatLng(LatLng latLng) {
        this.latLng = latLng;
    }
}

纬度:

public  class LatLng implements Serializable{       
    private String lat;
    private String lon;

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLon() {
        return lon;
    }

    public void setLon(String lon) {
        this.lon = lon;
    }
}

【讨论】:

    【解决方案2】:
    public  class LatLng {       
        public double latitude;
        public double longitude;
    
        public LatLng(){}
    }
    

    您可能还需要从 Request 类中删除其他构造函数

    public  class Request {       
        public String driverId;
        public LatLng latLng;
    
        public Request(){}
    }
    

    当您将类变量声明为public 时,您不必添加getter。您可以简单地获取这些值,例如 request.driverId

    【讨论】:

    • getter 不会导致此错误,否则错误消息会有所不同。
    猜你喜欢
    • 2012-06-24
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2020-10-01
    • 2019-09-28
    • 1970-01-01
    相关资源
    最近更新 更多