我一直在尝试构建一个带有应用内付款的Android应用程序,但设置起来有困难。 我正在调用以下内容:
Activity activity = new Activity();
billingClient = BillingClient.newBuilder(this).setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingResponse int billingResponseCode) {
if (billingResponseCode == BillingResponse.OK) {
List<String> skuList = new ArrayList<> ();
skuList.add("android.test.purchased");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(SkuType.INAPP);
billingClient.querySkuDetailsAsync(params.build(),
(responseCode, skuDetailsList) -> {
if (responseCode == BillingResponse.OK
&& skuDetailsList != null) {
for (SkuDetails skuDetails : skuDetailsList) {
System.out.println("The price is "+skuDetails.getPrice());
BillingFlowParams flowParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build();
int code = billingClient.launchBillingFlow(activity,flowParams);
System.out.println("Code is: "+code);
}
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
System.out.print("Billing disconnected?");
}
});
当我运行这段代码时,返回值是-1,并且出现了错误:
Exception while launching billing flow: ; for sku: android.test.purchased; try to reconnect
。我发现错误代码-1表示:“Play Store服务未连接 - 可能是短暂状态”(https://developer.android.com/reference/com/android/billingclient/api/BillingClient.BillingResponse)。我尝试使用测试产品ID(android.test.purchased
)以及我自己创建的真实产品ID,但结果相同。我还尝试重启(物理)设备,但问题依然存在。我可以正常访问Play Store应用程序,因此无法找出错误所在。