querySkuDetailsAsync在尝试测试Android的应用内购买时收到-1的responseCode

时间:2018-05-16 02:05:26

标签: android in-app-purchase

我正在使用Android应用,并希望进行应用内购买。为了测试这些,我发布了一个只发给我自己的Alpha版本。当我尝试查询产品详细信息时,我得到的响应代码为-1,根据文档意味着该服务已断开连接。我的相关代码:

mBillingClient = BillingClient.newBuilder(getContext()).setListener(new PurchasesUpdatedListener() {
        @Override
        public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
            Log.d(TAG, "Purchase Updated!");
        }
    }).build();

    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(int billingResponseCode) {
            if (billingResponseCode == BillingClient.BillingResponse.OK) {
                Log.d(TAG, "Billing setup complete!");
            }
        }
        @Override
        public void onBillingServiceDisconnected() {
            Log.d(TAG, "DISCONNECTED!");
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
    });

    List skuList = new ArrayList<> ();
    skuList.add("plaque001");
    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
    params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
    mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
        @Override
        public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
            Log.d(TAG, "details response received!");
            Log.d(TAG, Integer.toString(responseCode));
        }
    });

每次打开这个片段,我都会看到日志:

details response received!
-1
Billing setup complete!

当我使用从Play商店下载的Alpha版本时,会出现这种情况,我在Play控制台上有一个产品,其ID为“plaque001”,标记为活动状态。 我究竟做错了什么? 我是否错过了设置和发布应用内购买的步骤? 如何测试以确保产品正常工作?

1 个答案:

答案 0 :(得分:0)

startConnection是一个异步呼叫,您正在尝试querySkuDetailsAsync连接BillingClient之前。

mBillingClient.startConnection(new BillingClientStateListener() {
    @Override
    public void onBillingSetupFinished ( int billingResponseCode){
        if (billingResponseCode == BillingClient.BillingResponse.OK) {
            Log.d(TAG, "Billing setup complete!");

           //TODO query here. (extract into a separate method)
        }
    }
});