如何在Android中特别从Google Play商店获取应用购买产品中的“购买状态”?

时间:2019-10-28 16:17:44

标签: android in-app-billing

我正在使用Android应用程序中的Google API客户端库来获取有关购买状态的信息。

当我在Android Studio中将应用程序运行到模拟器或物理设备上时,它运行良好。

但是,生成签名的apk /捆绑包后,它不起作用。这是一个错误。

“ product = Publisher.purchases()。products()。get(getPackageName(),purchase.getSku(),purchase.getPurchaseToken())。execute();

purchaseState = product.getPurchaseState();“

    com.google.api.client.http.HttpTransport httpTransport;
    JsonFactory jsonFactory;
    GoogleCredential credential;
    AndroidPublisher publisher;
 httpTransport = null;
httpTransport = new com.google.api.client.http.javanet.NetHttpTransport();
        jsonFactory = JacksonFactory.getDefaultInstance();
        try {
            credential = GoogleCredential.fromStream(getResources().openRawResource(R.raw.key)).createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));
        } catch (IOException e) {
            e.printStackTrace();
        }
        publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential).setApplicationName(getString(R.string.app_name)).build();

        billingClient = BillingClient.newBuilder(this).setListener(this).enablePendingPurchases().build();
    billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

                    billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP,
                            new PurchaseHistoryResponseListener() {
                                @Override
                                public void onPurchaseHistoryResponse(BillingResult billingResult,
                                                                      List<PurchaseHistoryRecord> purchasesList) {

                                    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
                                            && purchasesList.size() >0) {

                                        GetPurchaseDetails asyncTask = new GetPurchaseDetails(purchasesList,action);
                                        asyncTask.execute();

                                    }
                                }
                            });
                } 
            }
            @Override
            public void onBillingServiceDisconnected() {

            }
        });

 public class GetPurchaseDetails extends AsyncTask<Integer, Void, Boolean> {
        List<PurchaseHistoryRecord> purchasesList;
        String action;

        private GetPurchaseDetails(List<PurchaseHistoryRecord> purchasesList,String action) {
            this.purchasesList = purchasesList;
            this.action = action;

        }

        @Override
        protected Boolean doInBackground(Integer... params) {
            ProductPurchase product ;
            Integer purchaseState;
            for (PurchaseHistoryRecord purchase : purchasesList) {
                try {
                    product = publisher.purchases().products().get(getPackageName(), purchase.getSku(), purchase.getPurchaseToken()).execute();// Here Giving me an Exception 
                    purchaseState = product.getPurchaseState();

                    if (purchaseState == 0){
                        return true;
                    }
                } catch (IOException e) {
                    Log.i("!!!!Exception",""+e.getMessage());
                    e.printStackTrace();
                }
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            HideProgressDialog();
            if (result){


            }else {


            }
        }
    }

错误消息是:找不到404。     找不到

0 个答案:

没有答案