JsonMappingException:找不到适合类型的构造函数
JsonMappingException: No suitable constructor found for type
我正在使用 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);
}
}
}
我收到以下错误
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.google.android.gms.maps.model.LatLng]: can not instantiate from JSON object (need to add/enable type information?)
这是我的 POJO class
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
}
}
我应该如何设计我的响应 class 以与 "latLng" 一起使用,我们将不胜感激。
您必须构建两个不同的模型: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;
}
}
public class LatLng {
public double latitude;
public double longitude;
public LatLng(){}
}
您可能还需要从 Request
class
中删除其他构造函数
public class Request {
public String driverId;
public LatLng latLng;
public Request(){}
}
当您将 class 变量声明为 public
时,您不必添加 getter。您可以简单地获取这些值,例如 request.driverId
我正在使用 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);
}
}
}
我收到以下错误
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.google.android.gms.maps.model.LatLng]: can not instantiate from JSON object (need to add/enable type information?)
这是我的 POJO class
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
}
}
我应该如何设计我的响应 class 以与 "latLng" 一起使用,我们将不胜感激。
您必须构建两个不同的模型: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;
}
}
public class LatLng {
public double latitude;
public double longitude;
public LatLng(){}
}
您可能还需要从 Request
class
public class Request {
public String driverId;
public LatLng latLng;
public Request(){}
}
当您将 class 变量声明为 public
时,您不必添加 getter。您可以简单地获取这些值,例如 request.driverId