2016-11-07 6 views
1

私は後付でこのクラスをdeserialiseしようとしている:あなたが見ることができるように、私はのLocalDateTimeを使用していRetrofitでThreeTen LocalDateTimeをどのように逆シリアル化するのですか?

data class Measurement(val id: Long, val value: Float, val dateTime: LocalDateTime, val trashCanId: Long) : Parcelable { 
    companion object { 
     @JvmField val CREATOR: Parcelable.Creator<Measurement> = object : Parcelable.Creator<Measurement> { 
      override fun createFromParcel(source: Parcel): Measurement = Measurement(source) 
      override fun newArray(size: Int): Array<Measurement?> = arrayOfNulls(size) 
     } 
    } 

    constructor(source: Parcel) : this(source.readLong(), source.readFloat(), source.readSerializable() as LocalDateTime, source.readLong()) 

    override fun describeContents() = 0 

    override fun writeToParcel(dest: Parcel?, flags: Int) { 
     dest?.writeLong(id) 
     dest?.writeFloat(value) 
     dest?.writeSerializable(dateTime) 
     dest?.writeLong(trashCanId) 
    } 

} 

、それはThreeTenからです。まあ、私は私のサーバー

{ 
     "id": 1, 
     "value": 50.6, 
     "dateTime": { 
      "hour": 2, 
      "minute": 6, 
      "second": 9, 
      "nano": 0, 
      "dayOfYear": 308, 
      "dayOfWeek": "THURSDAY", 
      "month": "NOVEMBER", 
      "dayOfMonth": 3, 
      "year": 2016, 
      "monthValue": 11, 
      "chronology": { 
      "id": "ISO", 
      "calendarType": "iso8601" 
      } 
     } 
     } 

まあからこれを受け、私のdateTimeはいつもヌルである私は、データの解析方法レトロフィットが知っdoesntのことを信じて、または私は何かが足りないのですか?

回避策はありますか?

ご協力いただきましてありがとうございます。

答えて

0

BsedはManojさんFrekzzの答えに、私は私の解決策を見つけた...

まず私はDateTimeDtoを作成しました。

data class DateTimeDto(val dayOfYear : Int, val dayOfWeek : String, val month : String, val dayOfMonth : Int, 
         val year : Int, val monthValue : Int, val hour : Int, val minute : Int, val second : Int, 
         val nano : Int, val chronology : Chronology) 



fun toDateTime() : LocalDateTime{ 
    return LocalDateTime.of(year, monthValue, dayOfMonth, hour, minute) 
} 

そして私からのインターフェイスの署名を変更:

@GET("trashCan") 

fun getLocalDateTime() : Observable<List<LocalDateTime>> 

へ:

@GET("trashCan") 

fun getLocalDateTime() : Observable<List<DateTimeDto>> 

その後、私のオブザーバーに私がやったこと:

ServiceGenerator.createService(ApiClient::class.java) 
       .getLocalDateTime 
       .map { it.map { it.toLocalDateTime() } } 

そして、それはうまく働いた...私はRetrofitを自動的に逆シリアル化することができますが、これは良い回避策です

1

その後

public class DemoResponse { 
@com.google.gson.annotations.SerializedName("id") 
public int id; 
@com.google.gson.annotations.SerializedName("value") 
public double value; 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public double getValue() { 
    return value; 
} 

public void setValue(double value) { 
    this.value = value; 
} 

public Datetime getDatetime() { 
    return datetime; 
} 

public void setDatetime(Datetime datetime) { 
    this.datetime = datetime; 
} 

@com.google.gson.annotations.SerializedName("dateTime") 
public Datetime datetime; 

public static class Chronology { 
    @com.google.gson.annotations.SerializedName("id") 
    public String id; 
    @com.google.gson.annotations.SerializedName("calendarType") 
    public String calendartype; 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getCalendartype() { 
     return calendartype; 
    } 

    public void setCalendartype(String calendartype) { 
     this.calendartype = calendartype; 
    } 
} 

public static class Datetime { 
    @com.google.gson.annotations.SerializedName("hour") 
    public int hour; 
    @com.google.gson.annotations.SerializedName("minute") 
    public int minute; 
    @com.google.gson.annotations.SerializedName("second") 
    public int second; 
    @com.google.gson.annotations.SerializedName("nano") 
    public int nano; 
    @com.google.gson.annotations.SerializedName("dayOfYear") 
    public int dayofyear; 
    @com.google.gson.annotations.SerializedName("dayOfWeek") 
    public String dayofweek; 
    @com.google.gson.annotations.SerializedName("month") 
    public String month; 
    @com.google.gson.annotations.SerializedName("dayOfMonth") 
    public int dayofmonth; 
    @com.google.gson.annotations.SerializedName("year") 
    public int year; 
    @com.google.gson.annotations.SerializedName("monthValue") 
    public int monthvalue; 
    @com.google.gson.annotations.SerializedName("chronology") 
    public Chronology chronology; 

    public int getHour() { 
     return hour; 
    } 

    public void setHour(int hour) { 
     this.hour = hour; 
    } 

    public int getMinute() { 
     return minute; 
    } 

    public void setMinute(int minute) { 
     this.minute = minute; 
    } 

    public int getSecond() { 
     return second; 
    } 

    public void setSecond(int second) { 
     this.second = second; 
    } 

    public int getNano() { 
     return nano; 
    } 

    public void setNano(int nano) { 
     this.nano = nano; 
    } 

    public int getDayofyear() { 
     return dayofyear; 
    } 

    public void setDayofyear(int dayofyear) { 
     this.dayofyear = dayofyear; 
    } 

    public String getDayofweek() { 
     return dayofweek; 
    } 

    public void setDayofweek(String dayofweek) { 
     this.dayofweek = dayofweek; 
    } 

    public String getMonth() { 
     return month; 
    } 

    public void setMonth(String month) { 
     this.month = month; 
    } 

    public int getDayofmonth() { 
     return dayofmonth; 
    } 

    public void setDayofmonth(int dayofmonth) { 
     this.dayofmonth = dayofmonth; 
    } 

    public int getYear() { 
     return year; 
    } 

    public void setYear(int year) { 
     this.year = year; 
    } 

    public int getMonthvalue() { 
     return monthvalue; 
    } 

    public void setMonthvalue(int monthvalue) { 
     this.monthvalue = monthvalue; 
    } 

    public Chronology getChronology() { 
     return chronology; 
    } 

    public void setChronology(Chronology chronology) { 
     this.chronology = chronology; 
    } 
    } 
    } 

次にあなたが改造から、このクラスのオブジェクトへの応答を割り当て、および取得することができ、JSONレスポンスを保持するために、このクラスを追加し、このGradleの最初の

compile 'com.google.code.gson:gson:2.8.0' 

を追加します。設定値を使用して値を設定します。

+1

ありがとうManoj、私は数時間でコードを試します –

関連する問題