我正在尝试使用最新的 Google Play Billing 库(版本 2.0.1)实现应用内购买。
• 我已将产品 ID 添加到 skuList 中,并在发布 apk 到内部应用程序测试后在 Google Play 控制台中进行了设置。
• 但是当我启动 billingFlow 时,会出现以下情况:
2019-07-01 13:17:02.436 1225-1225/com.mypackage.myapp D/InAppBilling: Other code5
2019-07-01 13:17:02.436 1225-1225/com.mypackage.myapp D/InAppBilling: Invalid SKU input params. SKU can't be null
以下是我的代码:
List skuList = new ArrayList<>();
skuList.add("product_1");
skuList.add("product_2");
skuList.add("product_3");
skuList.add("product_"4);
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
mBillingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& skuDetailsList != null) {
for (Object skuDetailsObject : skuDetailsList) {
skuDetails = (SkuDetails) skuDetailsObject;
String sku = skuDetails.getSku();
String price = skuDetails.getPrice();
if ("product_1".equals(sku)) {
textA.setText(price);
} else if ("product_2".equals(sku)) {
textB.setText(price);
} else if ("product_3".equals(sku)) {
textC.setText(price);
} else if ("product_4".equals(sku)) {
textD.setText(price);
}
} else {
Log.d(TAG, "Sku is null");
}
Log.d(TAG, "i got response");
Log.d(TAG, String.valueOf(billingResult.getResponseCode()));
Log.d(TAG, billingResult.getDebugMessage());
}
});
mBuyButton = findViewById(R.id.pay);
mBuyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build();
mBillingClient.launchBillingFlow(PayActivity.this, flowParams);
}
});
这里是日志记录(logcat):
2019-07-01 13:17:04.173 1225-1225/com.mypackage.myapp D/InAppBilling: Sku is null
2019-07-01 13:17:04.173 1225-1225/com.mypackage.myapp D/InAppBilling: i got response
2019-07-01 13:17:04.173 1225-1225/com.mypackage.myapp D/InAppBilling: -1 //billing result response code
2019-07-01 13:17:04.173 1225-1225/com.mypackage.myapp D/InAppBilling: Service connection is disconnected. //debug message
我还试过使用保留的测试产品ID android.test.purchase,但是出现了同样的错误。
我也上传了应用到Play控制台(内部测试),但那里也无法正常工作。
非常感谢任何帮助...
编辑: 经过多次实验,我发现计费服务被断开了,但原因并不是下面这个: https://developer.android.com/reference/com/android/billingclient/api/BillingClient.BillingResponse#service_disconnected (-1)
mBillingClient = BillingClient.newBuilder(PayActivity.this).setListener(this).enablePendingPurchases().build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
Log.d(TAG, "Connection finished");
}
@Override
public void onBillingServiceDisconnected() {
//TODO implement your own retry policy
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
mBillingClient.startConnection(this);
}
});
在收到响应后,将调用onBillingSetupFinished函数。