简体   繁体   English

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

[英]GWT HashMap.put() not adding data to the HashMap

I have a game created in LibGDX that uses GWT to build an HTML5 version.我有一个在 LibGDX 中创建的游戏,它使用 GWT 来构建 HTML5 版本。 I've got a hashmap that I use for logging purchases and some purchases were being processed multiple times.我有一个用于记录购买的哈希图,并且一些购买被多次处理。 Looking into it I discovered it's because they weren't being added to the hashmap.查看它我发现这是因为它们没有被添加到哈希图中。 The logic that adds processes to the hashmap is as follows (and works fine on Desktop, iOS and Android versions):向 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);
}

This produces the following output这会产生以下输出

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

Specifically note log lines 4 and 5, the hashmap does not contain the key orderNo .特别注意日志第 4 行和第 5 行,哈希图不包含键orderNo I then call put and on the next line the hashmap still doesn't contain the key orderNo .然后我调用put并在下一行哈希图仍然不包含键orderNo

This one really has me stumped.这个真的让我难住了。 I use hashmaps all over the game and this is the only one that isn't working as expected.我在整个游戏中都使用哈希图,这是唯一一个没有按预期工作的。 There are no errors reported in the console and nothing else unexpected appears to be happening.控制台中没有报告错误,并且似乎没有其他意外发生。

Edit编辑

I decided to try this:我决定试试这个:

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

It just gets stuck in an infinite loop.它只是陷入无限循环。 It's a very stubborn HashMap !这是一个非常顽固的HashMap

Edit编辑

Next interesting point.下一个有趣的点。 The String that is used for orderNo is returned from the Facebook SDK as part of a JavaScriptObject .用于orderNoString作为JavaScriptObject一部分从 Facebook SDK 返回。 The code for this is below:代码如下:

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;
    }-*/;
}

I get an exception when trying to do indexOf() on this this string with the message that the indexOf() method doesn't exist.尝试对此字符串执行indexOf()时出现异常,并显示indexOf()方法不存在的消息。 indexOf() does exist and works perfectly well in GWT on normal Strings. indexOf()确实存在并且在普通字符串的 GWT 中工作得很好。 What's special about this String?这个字符串有什么特别之处? I've tried doing new String(orderNo) and the new String doesn't work as expected either.我试过做 new String(orderNo)并且新 String 也没有按预期工作。

Worked it out!解决了! According the Facebook example the JSON response is as follows:根据Facebook 示例,JSON 响应如下:

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

But on inspecting the actual JSON response it's this:但是在检查实际的 JSON 响应时是这样的:

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

Note the crucial difference.注意关键的区别。 payment_id is a numeric value and not a String hence the missing functions and the fact that it wasn't performing as a String should. payment_id是一个数值而不是一个String因此缺少函数以及它没有像String那样执行的事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM