2017-08-06 174 views
0

我使用xStream來使用某些JSON。多年來,我已經非常廣泛地使用了xstream。然而,這個問題讓我難堪。Xstream ConversionException錯誤輸入字符串

我得到以下ConversionException ...

com.thoughtworks.xstream.converters.ConversionException: For input string: ".232017E.232017E44" 
---- Debugging information ---- 
message    : For input string: ".232017E.232017E44" 
cause-exception  : java.lang.NumberFormatException 
cause-message  : For input string: ".232017E.232017E44" 
class    : java.sql.Timestamp 
required-type  : java.sql.Timestamp 
converter-type  : com.etepstudios.xstream.XStreamTimestampConverter 
line number   : -1 
class[1]   : com.pbp.bookacall.dataobjects.AppleReceipt 
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter 
class[2]   : com.pbp.bookacall.dataobjects.AppleReceiptCollection 
version    : 1.4.10 
------------------------------- 
at com.etepstudios.xstream.XStreamTimestampConverter.unmarshal(XStreamTimestampConverter.java:87) 

在我XStreamTimestampConverter I類打印出試圖要轉換的價值..果然是以下...

XStreamTimestampConverter value = 2017-08-05 23:44:23.GMT 

這裏是我的轉換器的解組功能...

public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) 
{ 
    Timestamp theTimestamp; 
    Date theDate; 

    String value = reader.getValue(); 

    try 
    { 
     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.Z"); 

     theDate = formatter.parse(value); 

     theTimestamp = new Timestamp (theDate.getTime()); 
    } 
    catch (Exception e) 
    { 
     System.out.println ("XStreamTimestampConverter value = " + value); 

     throw new ConversionException(e.getMessage(), e); 
    } 

    return theTimestamp; 
} 

任何想法,這個奇怪的字符串來自哪裏?它不存在於我的JSON中的任何地方。 xstream是否有一些奇怪的東西?[num] E. [num] E [num]表示法是什麼?這些數字可以隨着我每次運行這個而改變。此外,我也會得到一個For輸入字符串:「」。然而,價值與上述相似。這就像它隨機獲得某個地方的奇數值。

該數據來源於Apple的In-App Purchase/VerifyReceipt網絡會話。該系統工作得很好,但其他人卻沒有。同樣重要的是要注意,在這種情況下,它使用該轉換器解析了100個其他日期/時間戳字符串。它只是讓人困惑。也許是由於數據的大小?

回答

0

所以我想通了這裏發生了什麼。上面的解組函數不完全像我在代碼中所做的那樣...

SimpleDateFormat格式器實際上是在類中設置的,而不是在解組方法中。因此,如果Xstream持有我的轉換器的實例,並且跨多個線程調用unmarshal,那麼格式化程序可能會因爲它是同一個對象而感到困惑。

這是我在這一點上唯一的猜測,因爲將格式化程序初始化移入方法可解決問題。我會說SimpleDateFormatter不是線程安全的?

這是數據的純粹和它被同時調用的次數暴露了這個問題。只要給他人一個提示,以防萬一發生這種情況。