我使用 e BillingClient.queryProductDetailsAsync(queryProductDetailsAsync) 加载产品,但偶尔我从 onSkuDetailsResponse(onProductDetailsResponse) 收到一个空列表,即使 billingResult 没问题。为什么会这样?
有大约0.1%的概率会出现这种情况
private int loadProductsNew(LuaState L) {
if (!initSuccessful()) {
Log.w("Corona", "Please call init before trying to load products.");
return 0;
}
int managedProductsTableIndex = 1;
int listenerIndex = 2;
final List<QueryProductDetailsParams.Product> productList = new ArrayList<>();
final HashSet<String> managedProducts = new HashSet<String>();
if (L.isTable(managedProductsTableIndex)) {
int managedProductsLength = L.length(managedProductsTableIndex);
for (int i = 1; i <= managedProductsLength; i++) {
L.rawGet(managedProductsTableIndex, i);
if (L.type(-1) == LuaType.STRING) {
String id = L.toString(-1);
managedProducts.add(id);
productList.add(QueryProductDetailsParams.Product.newBuilder()
.setProductType(BillingClient.ProductType.INAPP)
.setProductId(id).build());
}
L.pop(1);
}
} else {
Log.e("Corona", "Missing product table to store.loadProducts");
}
final HashSet<String> subscriptionProducts = new HashSet<String>();
if (!CoronaLua.isListener(L, listenerIndex, "productList") && L.isTable(listenerIndex)) {
int subscriptionProductsLength = L.length(listenerIndex);
for (int i = 1; i <= subscriptionProductsLength; i++) {
L.rawGet(listenerIndex, i);
if (L.type(-1) == LuaType.STRING) {
String id = L.toString(-1);
subscriptionProducts.add(id);
productList.add(QueryProductDetailsParams.Product.newBuilder()
.setProductType(BillingClient.ProductType.SUBS)
.setProductId(id).build());
}
L.pop(1);
}
listenerIndex++;
}
final int listener = CoronaLua.isListener(L, listenerIndex, "productList") ? CoronaLua.newRef(L, listenerIndex) : CoronaLua.REFNIL;
QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
.setProductList(productList)
.build();
fBillingClient.queryProductDetailsAsync(params, new ProductDetailsResponseListener() {
public void onProductDetailsResponse(BillingResult billingResult, List<ProductDetails> productDetailsList) {
if (productDetailsList != null) {
// Process the result
for (ProductDetails details : productDetailsList) {
fCachedSKUDetails.put(details.getProductId(), details);
}
fDispatcher.send(new ProductListRuntimeTask(productDetailsList, managedProducts, subscriptionProducts, billingResult, listener));
}
}
});
return 0;
}
private int loadProductsOld(LuaState L) {
if (!initSuccessful()) {
Log.w("Corona", "Please call init before trying to load products.");
return 0;
}
int managedProductsTableIndex = 1;
int listenerIndex = 2;
final HashSet<String> managedProducts = new HashSet<String>();
if (L.isTable(managedProductsTableIndex)) {
int managedProductsLength = L.length(managedProductsTableIndex);
for (int i = 1; i <= managedProductsLength; i++) {
L.rawGet(managedProductsTableIndex, i);
if (L.type(-1) == LuaType.STRING) {
managedProducts.add(L.toString(-1));
}
L.pop(1);
}
} else {
Log.e("Corona", "Missing product table to store.loadProducts");
}
final HashSet<String> subscriptionProducts = new HashSet<String>();
if (!CoronaLua.isListener(L, listenerIndex, "productList") && L.isTable(listenerIndex)) {
int subscriptionProductsLength = L.length(listenerIndex);
for (int i = 1; i <= subscriptionProductsLength; i++) {
L.rawGet(listenerIndex, i);
if (L.type(-1) == LuaType.STRING) {
subscriptionProducts.add(L.toString(-1));
}
L.pop(1);
}
listenerIndex++;
}
final int listener = CoronaLua.isListener(L, listenerIndex, "productList") ? CoronaLua.newRef(L, listenerIndex) : CoronaLua.REFNIL;
final List<SkuDetails> allSkus = new ArrayList<SkuDetails>();
final BillingResult.Builder result = BillingResult.newBuilder().setResponseCode(BillingResponseCode.OK);
final BillingUtils.SynchronizedWaiter waiter = new BillingUtils.SynchronizedWaiter();
final SkuDetailsResponseListener responder = new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> list) {
if (billingResult.getResponseCode() != BillingResponseCode.OK && result.build().getResponseCode() == BillingResponseCode.OK) {
result.setResponseCode(billingResult.getResponseCode());
result.setDebugMessage(billingResult.getDebugMessage());
}
if (list != null) {
allSkus.addAll(list);
for (SkuDetails details : list) {
fCachedSKUDetailsOld.put(details.getSku(), details);
}
}
waiter.Hit();
}
};
int tasks = 0;
if (!managedProducts.isEmpty()) {
tasks++;
List<String> l = new ArrayList<String>(managedProducts);
SkuDetailsParams params = SkuDetailsParams.newBuilder().setSkusList(l).setType(BillingClient.SkuType.INAPP).build();
fBillingClient.querySkuDetailsAsync(params, responder);
}
if (!subscriptionProducts.isEmpty()) {
tasks++;
List<String> l = new ArrayList<String>(subscriptionProducts);
SkuDetailsParams params = SkuDetailsParams.newBuilder().setSkusList(l).setType(BillingClient.SkuType.SUBS).build();
fBillingClient.querySkuDetailsAsync(params, responder);
}
waiter.Set(tasks, new Runnable() {
@Override
public void run() {
fDispatcher.send(new ProductListRuntimeTaskOld(allSkus, managedProducts, subscriptionProducts, result.build(), listener));
}
});
return 0;
}