2016-09-24 52 views
1

JSON数据预计BEGIN_ARRAY但STRING改造的Android

[ 
    { 
    "date": "25-09-2016", 
    "_id": { 
     "$oid": "57e601bbf2a9d92a445ef8a3" 
    }, 
    "createdAt": "2016-09-24 10:01:55 +0530", 
    "horoscope": [ 
     { 
     "date": "25-09-2016", 
     "sunsign": "aries", 
     "horoscope": "Testing.." 
     }, 
     { 
     "date": "25-09-2016", 
     "sunsign": "taurus", 
     "horoscope": "Testing.." 
     }, 
     { 
     "date": "25-09-2016", 
     "sunsign": "pisces", 
     "horoscope": "Testing.." 
     } 
    ] 
    } 
] 

HoroscopeModel.java:

public class HoroscopeModel { 

    private Date date; 
    private Date createdAt; 
    private List<HoroscopeModel> horoscope; 

    public List<HoroscopeModel> getHoroscope() { 
     return horoscope; 
    } 

    public void setHoroscope(List<HoroscopeModel> horoscope) { 
     this.horoscope = horoscope; 
    } 


    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    public Date getCreatedAt() { 
     return createdAt; 
    } 

    public void setCreatedAt(Date createdAt) { 
     this.createdAt = createdAt; 
    } 

    public List<HoroscopeModel> getHoroscopeList() { 
     return horoscope; 
    } 

    public void setHoroscopeList(List<HoroscopeModel> horoscopeList) { 
     this.horoscope = horoscopeList; 
    } 
} 

Horoscope.java

public class Horoscope { 

    Date date; 
    String sunshine; 
    String horoscope; 

    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    public String getSunShine() { 
     return sunshine; 
    } 

    public void setSunShine(String sunShine) { 
     this.sunshine = sunShine; 
    } 

    public String getHoroscope() { 
     return horoscope; 
    } 

    public void setHoroscope(String horoscope) { 
     horoscope = horoscope; 
    } 
} 

ApiInterface.java

public interface ApiInterface { 

    @GET("/get_horoscope") 
    Call<List<HoroscopeModel>> getHoroscope(); 
} 

改造:

private void setupRestClient() { 
    OkHttpClient.Builder client = new OkHttpClient.Builder(); 
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); 
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
    client.addInterceptor(loggingInterceptor); 


    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(API_URL) 
      .client(client.build()) 
      .addConverterFactory(GsonConverterFactory.create(getGson())) 
      .build(); 

    REST_CLIENT = retrofit.create(ApiInterface.class); 
} 

private Gson getGson() { 
    GsonBuilder builder = new GsonBuilder(); 
    builder.registerTypeAdapter(Date.class, new DateDeserializer()); 
    builder.setPrettyPrinting(); 
    return builder.create(); 
} 

DateDeserializer.java

public class DateDeserializer implements JsonDeserializer<Date> { 
    private static final String TAG = "DateDeserializer"; 
    private static final String[] DATE_FORMATS = new String[]{ 
      "yyy-MM-dd hh:mm:ss Z",//2016-09-24 08:18:56 +0530 
      "dd-MM-yyyy" 
    }; 

    @Override 
    public Date deserialize(JsonElement jsonElement, Type typeOF, 
          JsonDeserializationContext context) throws JsonParseException { 
     for (String format : DATE_FORMATS) { 
      try { 
       return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString()); 
      } catch (ParseException e) { 
      } 
     } 
     throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString() 
       + "\". Supported formats: " + Arrays.toString(DATE_FORMATS)); 
    } 
} 

随着一切都完美地建立它抛出一个错误说java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 183 path $[0].horoscope[0].horoscope,请让我知道有没有在我的代码的任何错误。提前致谢。

回答

2

的错误消息是相当不言自明的到位日期用String任何地方使用。

$[0].horoscope[0].horoscopeString,但您试图将其反序列化为List

这是因为在HoroscopeModel中,您将horoscope定义为List<HoroscopeModel>而不是List<Horoscope>

GSON试图的horoscope的值(一个String)反序列化为的HoroscopeModelhoroscope字段(List)代替的Horoscopehoroscope字段(一个String)。

HoroscopeModel,更改为以下领域:

private List<HoroscopeModel> horoscope; 

要这样:

private List<Horoscope> horoscope; 

(不要忘记修改相应的方法也)

0

Gosn不会直接将字符串转换为日期,它会按照以下方式定义特定的格式。

Gson gson = new GsonBuilder() 
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss") 
.create(); 

所以,你应该如下

public class Horoscope { 

    String date 

public class HoroscopeModel { 

    private String date; 
    private String createdAt; 
+0

我希望这不是问题,我已经有日期DateDeserializer附加到Gson,请看看我更新的问题。 –

相关问题