GWT HashMap.put()不向HashMap添加数据

时间:2016-04-26 13:28:54

标签: javascript java gwt hashmap

我在LibGDX中创建了一个使用GWT构建HTML5版本的游戏。我有一个用于记录购买的哈希映射,一些购买正在被多次处理。调查它我发现它是因为它们没有被添加到hashmap中。将进程添加到hashmap的逻辑如下(并且在Desktop,iOS和Android版本上正常工作):

public void updatePurchase(String orderNo, UserPurchase purchase) {
    Gdx.app.log("Double Purchase", "In updatePurchase " + orderNo);

    if (StringUtils.isNullOrEmpty(orderNo) || purchase == null) return;
    Gdx.app.log("Double Purchase", "1");

    synchronized (this) {
        Gdx.app.log("Double Purchase", "2");
        UserPurchase existingPurchase = purchases.get(orderNo);
        Gdx.app.log("Double Purchase", "3 " + (existingPurchase != null));
        if (existingPurchase == null) {
            Gdx.app.log("Double Purchase", "4 " + purchases.containsKey(orderNo));
            purchases.put(orderNo, purchase);
            Gdx.app.log("Double Purchase", "5 " + purchases.containsKey(orderNo));
            setUpdated();
        } else {
            Gdx.app.log("Double Purchase", "6");
            if (existingPurchase.status == UserPurchase.STATUS_NEEDS_PROCESSING && purchase.status == UserPurchase.STATUS_PROCESSED) {
                Gdx.app.log("Double Purchase", "7");
                existingPurchase.status = UserPurchase.STATUS_PROCESSED;
                Gdx.app.log("Double Purchase", "8");
                setUpdated();
            }
        }
    }

    Gdx.app.log("Double Purchase", "Leave updatePurchase " + orderNo);
}

这会产生以下输出

GWT: WBLOG: Double Purchase: In updatePurchase 787349198062445
GWT: WBLOG: Double Purchase: 1
GWT: WBLOG: Double Purchase: 2
GWT: WBLOG: Double Purchase: 3 false
GWT: WBLOG: Double Purchase: 4 false
GWT: WBLOG: Double Purchase: 5 false
GWT: WBLOG: Double Purchase: Leave updatePurchase 787349198062445

特别注意日志行4和5,hashmap不包含键orderNo。然后我调用put,在下一行,hashmap仍然不包含密钥orderNo

这个真让我难过。我在整个游戏中使用了哈希映射,这是唯一一个没有按预期工作的。控制台中没有报告任何错误,似乎没有其他任何意外情况发生。

修改

我决定试试这个:

        while (!purchases.containsKey(orderNo)) {
            Gdx.app.log("Double Purchase", "Trying to add purchase " + count++);
            purchases.put(orderNo, purchase);
        }

它只会陷入无限循环。这是一个非常顽固的HashMap

修改

下一个有趣的观点。用于String的{​​{1}}作为orderNo的一部分从Facebook SDK返回。代码如下:

JavaScriptObject

尝试对此字符串执行public class PurchaseResponse extends JavaScriptObject { protected PurchaseResponse() { } public final native String getPaymentId() /*-{ return this.payment_id; }-*/; public final native double getAmount() /*-{ return this.amount; }-*/; public final native String getCurrency() /*-{ return this.currency; }-*/; public final native int getQuantity() /*-{ return this.quantity; }-*/; public final native String getRequestId() /*-{ return this.request_id; }-*/; public final native String getStatus() /*-{ return this.status; }-*/; public final native String getSignedRequest() /*-{ return this.signed_request; }-*/; } 时出现异常,并显示indexOf()方法不存在的消息。 indexOf()确实存在并且在普通字符串上的GWT中运行良好。这个字符串有什么特别之处?我尝试过新的indexOf()并且新的String也没有按预期工作。

1 个答案:

答案 0 :(得分:1)

解决了!根据{{​​3}},JSON响应如下:

{
  payment_id: "495869257196092",
  amount: "5.00",
  currency: "USD",
  quantity: "1", 
  request_id: "60046727",
  status: "completed", 
  signed_request: "7QYHzKqKByA7fjiqJUh2bxFvEdqdvn0n_y1zYiyN6tg.eyJhbGCJxdWFudGl0eSI6IjEiLCJzdGF0dXMiOiJjb21wbGV0ZWQifQ"
}

但是在检查实际的JSON响应时,它是这样的:

{  
   "payment_id":795448840585614,
   "amount":"0.79",
   "currency":"GBP",
   "quantity":"1",
   "request_id":"xxxx",
   "status":"completed",
   "signed_request":"xxx"
}

注意关键的区别。 payment_id是一个数字值,而不是String因此缺少的函数以及它不能作为String执行的事实。