android应用内结算 – restoreTransactionInformation
作者:互联网
我正在尝试使用应用内结算功能将免费应用升级为“付费”版本.我使用this tutorial中的代码来处理官方开发人员的计费
对于像我这样的简单流程,网站过于复杂和混乱.
我的代码升级工作正常.
当我尝试添加一些东西来检查用户是否已经购买但是通过重新安装或清除数据(我不关心哪些)而丢失了他们的购买数据时,问题出现了.
在应用启动时,我会检查第一次运行后设置的标志.如果该标志不存在,则会向用户显示一个对话框,警告他们应用程序将检查以前的购买情况,当他们单击“确定”时,将调用restoreTransactionInformation方法.然后这会导致应用程序强制关闭.
因为在调试时或在模拟器上应用程序内计费不起作用,所以每次我想尝试代码时都必须发布应用程序的签名版本.当我尝试进行restoreTransactionInformation请求时,我无法知道应用程序退出的原因.有没有人知道我如何诊断它,或者什么可能导致我的应用程序死亡?或者一个如何使用restoreTransactionInformation方法的工作示例?
编辑:所以看起来RESTORE_TRANSACTIONS请求得到正确的响应,并返回我的测试购买的详细信息.不幸的是,它可以用它做任何事情,应用程序被强制关闭.这是在市场响应RESTORE_TRANSACTIONS请求后发生的事情的logcat(没有混淆代码):
I/BillingService( 6484): confirmTransaction()
D/Finsky ( 1884): [7] MarketBillingService.getPreferredAccount: com.hippypkg: Account from first account.
I/BillingService( 6484): current request is:**********
I/BillingService( 6484): RESTORE_TRANSACTIONS Sync Response code: RESULT_OK
D/WindowManagerImpl( 6484): finishRemoveViewLocked, mViews[0]: com.android.internal.policy.impl.PhoneWindow$DecorView@**********
W/InputManagerService( 1381): [unbindCurrentClientLocked] Disable input method client.
W/InputManagerService( 1381): [startInputLocked] Enable input method client.
D/NativeCrypto( 1884): returned from sslSelect() with result 1, error code 2
D/Finsky ( 1884): [1] MarketBillingService.sendResponseCode: Sending response RESULT_OK for request ********** to com.hippypkg.
I/BillingService( 6484): Received action: com.android.vending.billing.PURCHASE_STATE_CHANGED
I/BillingService( 6484): purchaseStateChanged got signedData: {"nonce":**********,"orders":[{"orderId":"**********","packageName":"com.hippypkg","productId":"hippy_upgrade_free_to_full","purchaseTime":1331476540000,"purchaseState":0}]}
I/BillingService( 6484): purchaseStateChanged got signature: **********==
I/BillingService( 6484): signedData: {"nonce":**********,"orders":[{"orderId":"**********","packageName":"com.hippypkg","productId":"hippy_upgrade_free_to_full","purchaseTime":1331476540000,"purchaseState":0}]}
I/BillingService( 6484): signature: **********==
I/BillingService( 6484): confirmTransaction()
I/BillingService( 6484): makerequestbundle success
I/BillingService( 6484): putstringarray success
D/Finsky ( 1884): [24] MarketBillingService.getPreferredAccount: com.hippypkg: Account from first account.
D/AndroidRuntime( 6484): Shutting down VM
W/dalvikvm( 6484): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
E/AndroidRuntime( 6484): FATAL EXCEPTION: main
E/AndroidRuntime( 6484): java.lang.RuntimeException: Unable to start receiver com.hippypkg.BillingReceiver: java.lang.NullPointerException
E/AndroidRuntime( 6484): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2144)
E/AndroidRuntime( 6484): at android.app.ActivityThread.access$2400(ActivityThread.java:135)
E/AndroidRuntime( 6484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1114)
E/AndroidRuntime( 6484): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 6484): at android.os.Looper.loop(Looper.java:150)
E/AndroidRuntime( 6484): at android.app.ActivityThread.main(ActivityThread.java:4385)
E/AndroidRuntime( 6484): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 6484): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 6484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
E/AndroidRuntime( 6484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
E/AndroidRuntime( 6484): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 6484): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 6484): at android.os.Parcel.readException(Parcel.java:1328)
E/AndroidRuntime( 6484): at android.os.Parcel.readException(Parcel.java:1276)
E/AndroidRuntime( 6484): at com.android.vending.billing.IMarketBillingService$Stub$Proxy.sendBillingRequest(IMarketBillingService.java:100)
E/AndroidRuntime( 6484): at com.hippypkg.BillingHelper.confirmTransaction(BillingHelper.java:152)
E/AndroidRuntime( 6484): at com.hippypkg.BillingHelper.verifyPurchase(BillingHelper.java:250)
E/AndroidRuntime( 6484): at com.hippypkg.BillingReceiver.purchaseStateChanged(BillingReceiver.java:41)
E/AndroidRuntime( 6484): at com.hippypkg.BillingReceiver.onReceive(BillingReceiver.java:23)
E/AndroidRuntime( 6484): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2103)
E/AndroidRuntime( 6484): ... 10 more
W/ActivityManager( 1381): Force finishing activity com.hippypkg/.Hippy
解决方法:
所以我终于弄明白了.
如果您查看Google文档中的应用内结算概述,则会说明:
RESTORE_TRANSACTIONS请求类型还会触发PURCHASE_STATE_CHANGED广播意图,该意图包含在购买请求期间发送的相同类型的交易信息,尽管您不需要使用CONFIRM_NOTIFICATIONS消息来响应此意图.
在正常的购买 – 确认交易周期中,当您请求购买In App Billing产品时,Google会发回一个包含大量字段的JSON.其中一个字段是’notification_id’.当谷歌发送PURCHASE_STATE_CHANGED意图时,它需要来自应用程序的CONFIRM_NOTIFICATIONS响应,其中包含一堆信息,包括notification_id.这里一切都很好.
当您从Google获得PURCHASE_STATE_CHANGED以获取来自应用的RESTORE_TRANSACTIONS请求时,问题就开始了.此JSON不包含notificaion_id字段.该库仍然以CONFIRM_NOTIFICATIONS响应,将notification_id数组添加到包中,在这种情况下,该数组为null.这就是导致NullPointerException的原因.
解:
我通过添加一个布尔值来修改BillingHelper.java类,以跟踪用户何时进行正常购买,以及何时想要restoreTransactions.
如果是restoreTransactions请求,我会将消息发送回处理程序并跳过confirmNotifications步骤.
编辑:
上述修复程序的代码位于BillingHelper.java中
我使用布尔标志来跟踪用户是否进行了RESTORE_TRANSACTIONS调用(isRestoreTransactions).
在BillingHelper.java的’verifyPurchase’方法中,我更改了代码如下:
protected static void verifyPurchase(String signedData, String signature) {
ArrayList<VerifiedPurchase> purchases = BillingSecurity.verifyPurchase(signedData, signature);
if(isRestoreTransaction)
{
/*
*
*Add some logic to retrieve the restored purchase product ID's from the 'purchases' array
*
*/
//Set the boolean to false
isRestoreTranscation = false;
//Send a message to the handler, informing it that purchases were restored
if(mCompletedHandler != null){
mCompletedHandler.sendEmptyMessage(0);
} else {
Log.e(TAG, "verifyPurchase error. Handler not instantiated. Have you called setCompletedHandler()?");
}
}
else
{
/*
*......
*......
*......
*Original method body here
*......
*......
*......
*/
}
}
标签:android,in-app-billing 来源: https://codeday.me/bug/20191005/1857854.html