我正在尝试使用改造和杰克逊来消费API以进行反序列化.标题“No Creators,如默认构造,存在”中出现的错误:无法从Object值反序列化(onFailure中不会出现基于委托或属性的Creator“).
这是我想要获取的JSON:
{ "data": { "repsol_ID": "1129", "name": "ES-MASSAMÁ", "latitude": "38.763733333", "longitude": "-9.258619444000001", "address": "RUA GENERAL HUMBERTO DELGADO, LT.16", "post_code": "2745-280", "location": "QUELUZ", "service_store": 1, "service_mechanical_workshop": 0, "service_restaurant": 0, "service_wash": 1 }}
这是我的HomeFragment:
onCreate(){ viewmodel.retrIEveStation().observe(this, Observer { dataBinding.favouriteStationTxt.text = it.name })}
这是我的viewmodel:
class Homeviewmodel @Inject constructor( private val stationRepository: StationRepository) : viewmodel() { private val station = mutablelivedata<Station>() fun retrIEveStation():liveData<Station> = station fun loadStations(stationID:Int){ stationRepository.getStationFromID(stationID,{ station.postValue(it)},{}) }}
这是我的存储库:
class StationRepository @Inject constructor(var apimanager: apimanager) { fun getStationFromID(stationID:Int,onSuccess: (Station)->Unit, one rror: (Exception)->Unit){ apimanager.getStation(stationID, onSuccess,onError) }}
这是我的API管理器(加入了几个API管理器)
class apimanager @Inject constructor( private val stationsapimanager: Stationsapimanager, ){ fun getStation(stationID: Int, onSuccess: (Station)->Unit, onFailure: (e: Exception)->Unit){ stationsapimanager.getStation(stationID,{onSuccess(it.data.todomain())},onFailure) }}
这是我的Stationapimanager
class Stationsapimanager @Inject constructor(private val stationAPIService: StationsAPIService){ fun getStation(stationID: Int, onSuccess: (StationResponse)->Unit, onFailure: (e: Exception)->Unit){ stationAPIService.getStation(stationID).enqueue(request(onSuccess, onFailure)) } private fun <T> request(onSuccess: (T)->Unit, onFailure: (e: Exception)->Unit)= object : Callback<T> { overrIDe fun onFailure(call: Call<T>, t: Throwable) { Log.d("error",t.message) onFailure(Exception(t.message)) } overrIDe fun onResponse(call: Call<T>, response: Response<T>) { Log.d("Success",response.body().toString()) if(response.isSuccessful && response.body() != null) onSuccess(response.body()!!) else onFailure(Exception(response.message())) } }}
这是我的STationsAPIService(基本URL在风格中)
@GET("{station_ID}") fun getStation(@Path("station_ID") stationID: Int): Call<StationResponse>
这是我的StationResponse
class StationResponse ( @JsonProperty("data") val data: Station)
这是我的车站模型
data class Station( val repsol_ID: String, val name: String, val latitude: String, val longitude: String, val address: String, val post_code: String, val location: String, val service_store: Boolean, val service_mechanical_workshop: Boolean, val service_restaurant: Boolean, val service_wash: Boolean)
这是我的DataMappers:
import com.repsol.repsolmove.network.movestationsAPI.model.Station as APIStationfun APIStation.todomain() = Station( repsol_ID.toInt(), name, latitude.todouble(), longitude.todouble(), address, post_code, location, service_store, service_mechanical_workshop, service_restaurant, service_wash)
解决方法:
试试以下型号.我使用http://www.jsonschema2pojo.org/来创建这些模型.
StationResponse.java
import java.util.HashMap;import java.util.Map;import com.fasterxml.jackson.annotation.JsonAnyGetter;import com.fasterxml.jackson.annotation.JsonAnySetter;import com.fasterxml.jackson.annotation.JsonIgnore;import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.annotation.JsonProperty;import com.fasterxml.jackson.annotation.JsonPropertyOrder;@JsonInclude(JsonInclude.Include.NON_NulL)@JsonPropertyOrder({"data"})public class StationResponse {@JsonProperty("data")private Data data;@JsonIgnoreprivate Map<String, Object> additionalPropertIEs = new HashMap<String, Object>();@JsonProperty("data")public Data getData() {return data;}@JsonProperty("data")public voID setData(Data data) {this.data = data;}@JsonAnyGetterpublic Map<String, Object> getAdditionalPropertIEs() {return this.additionalPropertIEs;}@JsonAnySetterpublic voID setAdditionalProperty(String name, Object value) {this.additionalPropertIEs.put(name, value);}}
Data.java
import java.util.HashMap;import java.util.Map;import com.fasterxml.jackson.annotation.JsonAnyGetter;import com.fasterxml.jackson.annotation.JsonAnySetter;import com.fasterxml.jackson.annotation.JsonIgnore;import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.annotation.JsonProperty;import com.fasterxml.jackson.annotation.JsonPropertyOrder;@JsonInclude(JsonInclude.Include.NON_NulL)@JsonPropertyOrder({"repsol_ID","name","latitude","longitude","address","post_code","location","service_store","service_mechanical_workshop","service_restaurant","service_wash"})public class Data {@JsonProperty("repsol_ID")private String repsolID;@JsonProperty("name")private String name;@JsonProperty("latitude")private String latitude;@JsonProperty("longitude")private String longitude;@JsonProperty("address")private String address;@JsonProperty("post_code")private String postCode;@JsonProperty("location")private String location;@JsonProperty("service_store")private Integer serviceStore;@JsonProperty("service_mechanical_workshop")private Integer serviceMechanicalWorkshop;@JsonProperty("service_restaurant")private Integer serviceRestaurant;@JsonProperty("service_wash")private Integer serviceWash;@JsonIgnoreprivate Map<String, Object> additionalPropertIEs = new HashMap<String, Object>();@JsonProperty("repsol_ID")public String getRepsolID() {return repsolID;}@JsonProperty("repsol_ID")public voID setRepsolID(String repsolID) {this.repsolID = repsolID;}@JsonProperty("name")public String getname() {return name;}@JsonProperty("name")public voID setname(String name) {this.name = name;}@JsonProperty("latitude")public String getLatitude() {return latitude;}@JsonProperty("latitude")public voID setLatitude(String latitude) {this.latitude = latitude;}@JsonProperty("longitude")public String getLongitude() {return longitude;}@JsonProperty("longitude")public voID setLongitude(String longitude) {this.longitude = longitude;}@JsonProperty("address")public String getAddress() {return address;}@JsonProperty("address")public voID setAddress(String address) {this.address = address;}@JsonProperty("post_code")public String getPostCode() {return postCode;}@JsonProperty("post_code")public voID setPostCode(String postCode) {this.postCode = postCode;}@JsonProperty("location")public String getLocation() {return location;}@JsonProperty("location")public voID setLocation(String location) {this.location = location;}@JsonProperty("service_store")public Integer getServiceStore() {return serviceStore;}@JsonProperty("service_store")public voID setServiceStore(Integer serviceStore) {this.serviceStore = serviceStore;}@JsonProperty("service_mechanical_workshop")public Integer getServiceMechanicalWorkshop() {return serviceMechanicalWorkshop;}@JsonProperty("service_mechanical_workshop")public voID setServiceMechanicalWorkshop(Integer serviceMechanicalWorkshop) {this.serviceMechanicalWorkshop = serviceMechanicalWorkshop;}@JsonProperty("service_restaurant")public Integer getServiceRestaurant() {return serviceRestaurant;}@JsonProperty("service_restaurant")public voID setServiceRestaurant(Integer serviceRestaurant) {this.serviceRestaurant = serviceRestaurant;}@JsonProperty("service_wash")public Integer getServiceWash() {return serviceWash;}@JsonProperty("service_wash")public voID setServiceWash(Integer serviceWash) {this.serviceWash = serviceWash;}@JsonAnyGetterpublic Map<String, Object> getAdditionalPropertIEs() {return this.additionalPropertIEs;}@JsonAnySetterpublic voID setAdditionalProperty(String name, Object value) {this.additionalPropertIEs.put(name, value);}}
总结 以上是内存溢出为你收集整理的android – 没有创建者,比如默认构造,存在):无法从Object值反序列化(没有基于委托或属性的Creator全部内容,希望文章能够帮你解决android – 没有创建者,比如默认构造,存在):无法从Object值反序列化(没有基于委托或属性的Creator所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)