2013-08-31 18 views
4

我使用的後續代碼來解析來自谷歌的價格Play應用結算:如何可以解析從谷歌價格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」,「29.99美元」,「MX $ 374.79」。我的方法在這種情況下不起作用。

是否有解決此問題的通用方法?

回答

6

檢查他們的應用內結算示例項目和修改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; 
    } 
} 
+0

文檔位於:http://developer.android.com/google/play/billing/billing_reference.html附註:我建議不要使用「 iabHelper「課程。它提供的價值很小(大多數是包裝JSON),並不總是最新的,並且忽略了這裏所看到的基本特徵。只需查看代碼並使用Json類自行編譯即可。 – osxdirk

4

隨着谷歌Play可能會返回貨幣格式的價格這是不支持java.text.NumberFormat中,我寫我自己的實現

public class Price { 
private double value; 
private String currency; 
private String pattern; 
private DecimalFormat decimalFormat; 

private Price() {} 

private static String currencyToDecimalFormat(String value, Price price) { 
    char decimalSeparator = '.'; 
    char groupingSeparator = 0; 
    if (value.length() >= 3) { 
     char[] chars = value.toCharArray(); 
     if (chars[chars.length - 2] == ',') { 
      decimalSeparator = ','; 
      chars[chars.length - 2] = '.'; 
     } else if (chars[chars.length - 3] == ',') { 
      decimalSeparator = ','; 
      chars[chars.length - 3] = '.'; 
     } 
     value = new String(chars); 
    } 

    if (value.contains(",")) { 
     groupingSeparator = ','; 
     value = value.replaceAll(",", ""); 
    } else if (value.contains(" ")) { 
     groupingSeparator = ' '; 
     value = value.replaceAll(" ", ""); 
    } else if (value.contains("\u00A0")) { 
     groupingSeparator = '\u00A0'; 
     value = value.replaceAll("\u00A0", ""); 
    } 

    DecimalFormatSymbols symbols = new DecimalFormatSymbols(); 
    if (groupingSeparator != 0) { 
     price.decimalFormat = new DecimalFormat("###,###.00"); 
     symbols.setGroupingSeparator(groupingSeparator); 
    } else { 
     price.decimalFormat = new DecimalFormat("######.00"); 
    } 

    symbols.setDecimalSeparator(decimalSeparator); 
    price.decimalFormat.setDecimalFormatSymbols(symbols); 
    return value.replaceAll(",", ""); 
} 

public static Price parsePrice(String priceFromGoogle) { 
    Price price = new Price(); 
    StringBuilder patternBuilder = new StringBuilder(); 
    Pattern pattern = Pattern.compile("(?:[0-9]{1,3})(?:[0-9,.\\s\u00A0]+)"); 
    Matcher matcher = pattern.matcher(priceFromGoogle); 
    matcher.find(); 
    String priceString = matcher.group(); 
    if (priceFromGoogle.indexOf(priceString) == 0) { 
     if (priceFromGoogle.length() != priceString.length()) { 
      price.currency = priceFromGoogle.substring(priceString.length()); 
     } else { 
      price.currency = ""; 
     } 
    } else { 
     price.currency = priceFromGoogle.substring(0, priceFromGoogle.indexOf(priceString)); 
    } 
    price.currency = price.currency.trim(); 


    if (priceFromGoogle.startsWith(price.currency)) { 
     patternBuilder.append("%1s"); 
     char nextChar = priceFromGoogle.charAt(price.currency.length()); 
     if (nextChar == ' ' || nextChar == 0xA0) { 
      patternBuilder.append(' '); 
     } 
     patternBuilder.append("%2$s"); 
    } else { 
     patternBuilder.append("%2$s"); 
     char prevChar = priceFromGoogle.charAt(priceFromGoogle.indexOf(price.currency) - 1); 
     if (prevChar == ' ' || prevChar == 0xA0) { 
      patternBuilder.append(' '); 
     } 
     patternBuilder.append("%1s"); 
    } 
    price.pattern = patternBuilder.toString(); 

    priceString = trim(priceString); 

    priceString = currencyToDecimalFormat(priceString, price); 

    price.value = Double.parseDouble(priceString); 
    return price; 
} 

@Override 
public String toString() { 
    if (pattern != null) { 
     return String.format(pattern, currency, decimalFormat.format(value)); 
    } else { 
     return ""; 
    } 
} 

}

EDIT1:由於

谷歌使用非破碎空間,而不是通常的空間,你需要檢查這一點,並使用自定義修剪功能:

public static String trim(String text) { 
    int start = 0, last = text.length() - 1; 
    int end = last; 
    while ((start <= end) && (text.charAt(start) <= ' ' || text.charAt(start) == 0xA0)) { 
     start++; 
    } 
    while ((end >= start) && (text.charAt(end) <= ' ' || text.charAt(end) == 0xA0)) { 
     end--; 
    } 
    if (start == 0 && end == last) { 
     return text; 
    } 
    return text.substring(start, end); 
} 
+0

實際上是否有任何使用逗號作爲小數點分隔符的示例? –

+0

逗號用作俄羅斯價格的小數點分隔符,例如105,95руб。 –

+0

您是否特意檢查了Google Play返回的格式,或者您是否一般地說? –

6

您可以通過IabHelper檢索的JSON中的micros得到價格。

這不是正式文件,但這裏是我如何通過編輯SkuDetails.java做到了:

public class SkuDetails { 
    ... 
    Double mPriceMicros; 

    public SkuDetails(String itemType, String jsonSkuDetails) throws JSONException { 
     ... 

     String priceMicros = o.optString("price_amount_micros"); 
     if (priceMicros != null) { 
      String format = new StringBuilder(priceMicros).insert(priceMicros.length() - 6, ".").toString(); 
      mPriceMicros = Double.parseDouble(format); 
     } 
    } 

    ... 
    public Double getPriceMicros() { return mPriceMicros; } 
} 

希望這有助於!

PS:我想你的艙位價格,但它解析0.80.89€

+0

它正式記錄在這裏:http://developer.android.com/google/play/billing/billing_reference .html – osxdirk

+1

爲什麼不簡單Double priceAmount = skuDetails.getPriceAmountMicros()/ 1000000.0 – voytez

相關問題