其他地方的序列化的SimpleXML反序列化对象使UnparseableDateException [英] SimpleXML deserializing object serialized elsewhere gives UnparseableDateException

查看:503
本文介绍了其他地方的序列化的SimpleXML反序列化对象使UnparseableDateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对简单的类。我序列化我的服务器上使用JDK 1.7,并使用我的客户在Android 2.2,这两者都是利用简单的XML 2.6.2反序列化。该类包含日期字段我与@Attribute注释。生成的XML看起来是这样的:

I have a relatively simple class. I serialize it using JDK 1.7 on my server, and deserialize it using Android 2.2 on my client, both of which are using simple xml 2.6.2. The class contains a Date field which I annotate with @Attribute. Resulting XML looks something like this:

<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>

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

When I try to deserialize it, the exception I get is:

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讨论该问题​​的可能性:无法解析的日期例外但我的问题是我怎么解决这个问题?有没有一种方法,使其避免使用马车code Android中我可以简单的XML解决数据处理?

There's a possibility that this is related to the problem discussed at Java: unparseable date exception but my question is how do I work around this issue? Is there a way I can fix the date processing in simple xml so that it avoids using the buggy code in Android?

更新1:

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

Interesting data point, here is a date from a document generated on the Android device:

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

这,似乎工作正常。所以,也许有办法说服服务器上的Java生产日期这种格式,而不是使用BST(或presumably其他类似的时区的名字)?

This, it seems, works fine. So, perhaps there's a way to persuade Java on the server to produce dates in this format, rather than using 'BST' (or presumably other similar timezone names)?

推荐答案

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

I never did find a way to tell the system to not use symbolic timezone names. I could almost get the right date format by using the RFC822-compatible timezone support in SimpleDateFormat, but it wasn't quite right. In the end, I simply decided to force the system to use times in 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);
    }
}

然后我使用时要求匹配日期类返回GMTDateTransform的静态实例的匹配器,并通过了以持留,当我创造了它:

I then used a Matcher that returned a static instance of GMTDateTransform when asked to match the Date class, and passed that to Persister when I created it:

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

(以上code是我的实际code所做的简化,因为我有其他的类进行自定义转换,并使用类的一个HashMap转换映射找到正确的)

(the above code is a simplification of what my actual code does, because I have other classes with custom transforms, and use a hashmap of class to transform mappings to find the correct one)

这篇关于其他地方的序列化的SimpleXML反序列化对象使UnparseableDateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆