1

我有一个相对简单的课程。我在我的服务器上使用 JDK 1.7 对其进行序列化,并在我的客户端上使用 Android 2.2 对其进行反序列化,两者都使用简单的 xml 2.6.2。该类包含一个我用@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”(或可能是其他类似的时区名称)?

4

1 回答 1

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;
        }
    });

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

于 2012-04-06T17:57:09.537 回答