gpt4 book ai didi

android - 如何从 Google Play 应用内结算中解析价格

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:06 28 4
gpt4 key购买 nike

我使用以下代码解析来自 Google Play 应用内结算的价格:

private static Number parsePrice(String priceFromGoogle) {
Locale currencyLocale = getCurrencyLocale(priceFromGoogle);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(currencyLocale);
Number number = null;
try {
number = numberFormat.parse(priceFromGoogle);
} catch (ParseException e) {
e.printStackTrace();
}
return number;
}

private Locale getCurrencyLocale(String price) {
Locale locale = null;
for (Locale availableLocale : Locale.getAvailableLocales()) {
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(availableLocale);
try {
numberFormat.parse(price);
locale = availableLocale;
break;
} catch (ParseException e) {
//do nothing
}
}
return locale;
}

它在我的测试设备和我的语言环境中运行良好。但在某些设备和某些国家/地区,我会遇到这样的价格:“Php1,337.07”、“US$ 29.99”、“MX$374.79”。我的方法在这种情况下不起作用。

是否有解决此问题的通用方法?

最佳答案

检查他们的应用内结算示例项目并修改 SkuDetails.java,以便您也可以获得该信息:

import org.json.JSONException;
import org.json.JSONObject;

/**
* Represents an in-app product's listing details.
*/
public class SkuDetails {
String mItemType;
String mSku;
String mType;
int mPriceAmountMicros;
String mPriceCurrencyCode;
String mPrice;
String mTitle;
String mDescription;
String mJson;

public SkuDetails(String jsonSkuDetails) throws JSONException {
this(IabHelper.ITEM_TYPE_INAPP, jsonSkuDetails);
}

public SkuDetails(String itemType, String jsonSkuDetails) throws JSONException {
mItemType = itemType;
mJson = jsonSkuDetails;
JSONObject o = new JSONObject(mJson);
mSku = o.optString("productId");
mType = o.optString("type");
mPrice = o.optString("price");
mPriceAmountMicros = o.optInt("price_amount_micros");
mPriceCurrencyCode = o.optString("price_currency_code");
mTitle = o.optString("title");
mDescription = o.optString("description");
}

public String getSku() { return mSku; }
public String getType() { return mType; }
public String getPrice() { return mPrice; }
public String getTitle() { return mTitle; }
public String getDescription() { return mDescription; }
public int getPriceAmountMicros() { return mPriceAmountMicros; }
public String getPriceCurrencyCode() { return mPriceCurrencyCode; }

@Override
public String toString() {
return "SkuDetails:" + mJson;
}
}

关于android - 如何从 Google Play 应用内结算中解析价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18547315/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com