GWT HashMap.put() 没有向 HashMap 添加数据

GWT HashMap.put() not adding data to the HashMap

我在 LibGDX 中创建了一个使用 GWT 构建 HTML5 版本的游戏。我有一个用于记录购买记录的哈希图,并且一些购买被处理了多次。调查它我发现这是因为它们没有被添加到哈希图中。将进程添加到 hashmap 的逻辑如下(在桌面、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 并且在下一行哈希图仍然不包含键 orderNo.

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

编辑

我决定试试这个:

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

它只是陷入了无限循环。很倔强HashMap!

编辑

下一个有趣的点。用于 orderNoString 作为 JavaScriptObject 的一部分从 Facebook SDK 返回。代码如下:

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() 方法不存在的消息。 indexOf() 确实存在并且在普通字符串上的 GWT 中运行良好。这个字符串有什么特别之处?我试过做 new String(orderNo),但 new String 也没有按预期工作。

解决了!根据 Facebook example 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 应该那样执行。