SimpleXML deserializing object serialized elsewhere gives UnparseableDateException

自古美人都是妖i 提交于 2019-12-11 07:55:16

问题


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)

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?

Update 1:

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

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


回答1:


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

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

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



来源:https://stackoverflow.com/questions/9926049/simplexml-deserializing-object-serialized-elsewhere-gives-unparseabledateexcepti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!