在其他地方序列化的SimpleXML反序列化对象会产生UnparseableDateException

时间:2012-03-29 13:11:31

标签: java android simple-framework

我有一个相对简单的课程。我在我的服务器上使用JDK 1.7对其进行序列化,并在我的客户端上使用Android 2.2对其进行反序列化,两者都使用简单的xml 2.6.2。该类包含一个Date字段,我用@Attribute注释。生成的XML看起来像这样:

<daySchedule id="e086b34c-2836-4ecb-af36-5764e3f44b21" date="2012-03-29 00:00:00.0 BST">
      <driver id="022cbb89-1226-4d85-ac28-b4848f3bc4ae" name="Julian"/>
      <job id="1e444bf0-59ec-44f6-8f94-01e8606caa27" scheduledStartTime="2012-03-27 23:05:00.0 BST">
         <location id="8c00f18a-fc97-4f2c-a369-ec4efe62f4bb" clientId="92797509-600c-47b4-989d-150c7e695e95" name="The Phantom Coach">
             <gpsPoint latitude="52.390499114990234" longitude="-1.5485700368881226"/>
         </location>
         <task id="c6adff66-b342-4d24-874a-e676ce720af8" locationId="8c00f18a-fc97-4f2c-a369-ec4efe62f4bb" name="Have a drink" description="Something non-alcoholic, probably Diet Pepsi."/>
       </job>
    </daySchedule>

当我尝试反序列化它时,我得到的例外是:

03-29 14:06:58.975: W/System.err(1583): java.text.ParseException: Unparseable date: 2012-03-29 00:00:00.0 BST
03-29 14:06:58.975: W/System.err(1583):     at java.text.DateFormat.parse(DateFormat.java:645)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.transform.DateType$DateFormat.getDate(DateType.java:189)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.transform.DateType.getDate(DateType.java:112)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.transform.DateTransform.read(DateTransform.java:75)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.transform.DateTransform.read(DateTransform.java:44)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.transform.Transformer.read(Transformer.java:104)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Support.read(Support.java:185)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.PrimitiveFactory.getInstance(PrimitiveFactory.java:105)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Primitive.readTemplate(Primitive.java:231)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Primitive.read(Primitive.java:171)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Primitive.read(Primitive.java:126)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.readVariable(Composite.java:687)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.readInstance(Composite.java:635)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.readAttribute(Composite.java:558)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.readAttributes(Composite.java:474)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.readSection(Composite.java:387)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.read(Composite.java:367)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.readDefault(Composite.java:262)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.read(Composite.java:232)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.read(Composite.java:202)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Composite.read(Composite.java:150)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Persister.read(Persister.java:632)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Persister.read(Persister.java:613)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Persister.read(Persister.java:591)
03-29 14:06:58.985: W/System.err(1583):     at org.simpleframework.xml.core.Persister.read(Persister.java:550)
03-29 14:06:58.995: W/System.err(1583):     at org.simpleframework.xml.core.Persister.read(Persister.java:451)
03-29 14:06:58.995: W/System.err(1583):     at net.meridiandigital.binco.demo.LocationList$2.doInBackground(LocationList.java:78)
03-29 14:06:58.995: W/System.err(1583):     at net.meridiandigital.binco.demo.LocationList$2.doInBackground(LocationList.java:1)
03-29 14:06:58.995: W/System.err(1583):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-29 14:06:58.995: W/System.err(1583):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-29 14:06:58.995: W/System.err(1583):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-29 14:06:58.995: W/System.err(1583):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-29 14:06:58.995: W/System.err(1583):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-29 14:06:58.995: W/System.err(1583):     at java.lang.Thread.run(Thread.java:1096)

这可能与Java: unparseable date exception中讨论的问题有关但我的问题是如何解决这个问题?有没有办法可以在简单的xml中修复日期处理,以避免在Android中使用错误的代码?

更新1:

有趣的数据点,这是Android设备上生成的文档的日期:

2012-03-30 07:00:38.552 GMT+01:00

这似乎工作正常。那么,也许有一种方法可以说服服务器上的Java以这种格式生成日期,而不是使用'BST'(或者可能是其他类似的时区名称)?

1 个答案:

答案 0 :(得分:3)

我从未找到告诉系统不使用符号时区名称的方法。我可以几乎通过使用SimpleDateFormat中与RFC822兼容的时区支持来获得正确的日期格式,但它并不完全正确。最后,我决定强制系统在GMT中使用时间:

private static final class GMTDateTransform implements Transform<Date>
{
    ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat> () {
        protected SimpleDateFormat initialValue ()
        {
            SimpleDateFormat r = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS zzz");
            r.setTimeZone (TimeZone.getTimeZone ("GMT"));
            return r;
        }
    };

    public Date read (String source) throws Exception 
    {
        return sdf.get ().parse (source);
    }
    public String write (Date source) throws Exception
    {
        return sdf.get ().format (source);
    }
}

然后我使用了一个Matcher,当要求匹配Date类时返回GMTDateTransform的静态实例,并在创建它时将其传递给Persister:

    final GMTDateTransform transform = new GMTDateTransform();
    return new Persister(new Matcher() {
        @Override
        public Transform match(Class cls) throws Exception {
            if (cls == Date.class) return transform;
            return null;
        }
    });

(上面的代码是我实际代码所做的简化,因为我有其他自定义转换类,并使用类的hashmap转换映射以找到正确的代码)