处理应用内计费的错误代码

问题描述 投票:0回答:1

我正试图在一个安卓应用程序中实现应用内计费。我已经阅读了文档,除了处理错误代码的部分,我已经完成了应用内计费的实现。文档中提到了以下内容

"服务器响应代码下面的表格列出了从Google Play发送到您的应用程序的所有服务器响应代码。Google Play 以映射到响应捆绑包中 RESPONSE_CODE 键的整数形式同步发送响应代码。您的应用程序必须处理所有这些响应代码。

表 1.应用内响应代码汇总 应用内计费 API 调用的响应代码摘要。"

enter image description here

所以我必须处理所有这些错误代码。我已经搜索了一个关于如何做到这一点的例子,但我没有找到一个。

谁能提供一个处理所有错误代码的例子,以便我可以遵循它?

这是接收响应错误代码的方法(可能是上表中的错误之一)。

 public void onError(int response, Exception e) {
        // handle errors here
        Log.w(LOG," in onError"+ "response is" + response);
    }

所以只要我从google文档中了解到这里,我必须处理以上所有的错误代码。

所以,我想看到一个例子。什么是最好的方式来做到这一点?

android in-app-billing
1个回答
0
投票

因为谷歌说,你必须处理所有的错误,并不意味着你必须。

其简单的偏好。我已经创建了超过20个应用程序与完整的工作计费功能,我处理错误的方式相同。BILLING_RESPONSE_RESULT_OK. 其余的我只是用其他的方法把它们组合在一起(因为我的用户不需要特定的错误)。

这是我的做法。

@Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    //do my response is OK code here
                    return;
                }
                //do my response is an ERROR code here
            }

但在你的情况下,你可以很容易地做到这一点,如果你想处理所有的错误。

                           @Override
                            public void onBillingSetupFinished(BillingResult billingResult) {
                                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                                    //do my response is OK code here
                                    return;
                                } else if (billingResult.getResponseCode() ==
    BillingClient.BillingResponseCode.USER_CANCELED) {
        //do user cancelled here
        } 

            else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
            //do user cancelled here
            } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE) {
            //do SERVICE_UNAVAILABLE here
            } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.BILLING_UNAVAILABLE) {
            //do BILLING_UNAVAILABLE here
        }}

等等等等。

PS. 做到以上几点,使用切换案例会更快,更优化。

© www.soinside.com 2019 - 2024. All rights reserved.