在 Apple 的 VerifyReceipt 上不断收到 21002 'java.lang.NullPointerException' [英] Keep getting 21002 'java.lang.NullPointerException' on Apple's VerifyReceipt
问题描述
当我尝试在沙箱中测试我的应用内购买时,我不断收到来自 Apple 的 21002 'java.lang.NullPointerException' 错误.这就是我所做的:
I keep getting 21002 'java.lang.NullPointerException' errors from Apple when I try to test my In-App Purchases in the sandbox. This is what I have done:
- 在 iTunes Connect 中设置应用内购买产品.我可以通过 StoreKit 成功检索它们.
- 在 iTunes Connect 中设置测试用户.
- 应用程序启动,下载可用的应用内购买产品,我触发应用内购买,Apple 用交易收据进行响应,我在 iPhone 上对其进行 base64 编码并发送到我的 C#/ASP.NET 服务器.
- 服务器将接收到的字符串放入 JSON(我使用
NewtonSoft.Json
和手动尝试过)并将 JSON 发送给 Apple:
- Set up the In-App Purchase products in iTunes Connect. I can successfully retrieve them via StoreKit.
- Set up test user in iTunes Connect.
- Application starts up, downloads the available In-App Purchase products, I trigger the In-App Purchase and Apple responds with the transactionReceipt, which I base64-encode on the iPhone and send to my C#/ASP.NET server.
- The server puts the received string into JSON (I tried it both with
NewtonSoft.Json
and manually) and sends the JSON to Apple:
var json = "{ 'receipt-data': '" +收据 + "'}";
或:
var json = new JObject(new JProperty("receipt-data",receipt)).ToString();
然后:
var webRequest = System.Net.HttpWebRequest.Create("https://sandbox.itunes.apple.com/verifyReceipt");
webRequest.ContentType = "text/plain";
webRequest.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(receipt);
webRequest.ContentLength = byteArray.Length;
using (var stream = webRequest.GetRequestStream())
{
stream.Write(byteArray, 0, byteArray.Length);
stream.Flush();
}
var resp = webRequest.GetResponse();
if (resp != null)
{
using (var sr = new System.IO.StreamReader(resp.GetResponseStream()))
{
var result = sr.ReadToEnd().Trim();
var iapResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<AppleIapResponse>(result);
// always getting '21002' 'java.lang.NullPointerException'
}
}
我尝试了一切:更改 ContentType、JSON 格式、编码....
I tried everything: changing the ContentType, the JSON formatting, the encoding....
有什么提示吗?
推荐答案
这是代码中的一个简单错误,我将收据写入 POST,而不是 JSON:
It's a simple bug in the code, I was writing the receipt into the POST, not the JSON:
byte[] byteArray = Encoding.UTF8.GetBytes(json);
^^^^
我的头撞到墙上很痛..
The wall hurts from banging my head against it..
这篇关于在 Apple 的 VerifyReceipt 上不断收到 21002 'java.lang.NullPointerException'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!