【问题标题】:Keep getting 21002 'java.lang.NullPointerException' on Apple's VerifyReceipt在 Apple 的 VerifyReceipt 上不断收到 21002 'java.lang.NullPointerException'
【发布时间】:2012-06-18 15:18:07
【问题描述】:

当我尝试在沙盒中测试我的应用内购买时,我不断收到来自 Apple 的 21002 'java.lang.NullPointerException' 错误。这就是我所做的:

  • 在 iTunes Connect 中设置应用内购买产品。我可以通过 StoreKit 成功检索它们。
  • 在 iTunes Connect 中设置测试用户。
  • 应用程序启动,下载可用的应用内购买产品,我触发应用内购买,Apple 以 transactionReceipt 响应,我在 iPhone 上对其进行 base64 编码并发送到我的 C#/ASP.NET 服务器。
  • 服务器将接收到的字符串放入 JSON 中(我用 NewtonSoft.Json 和手动尝试过)并将 JSON 发送给 Apple:

var json = "{ 'receipt-data': '" + receipt + "'}";

或者:

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 格式、编码......

有什么提示吗?

【问题讨论】:

  • 尝试将 ContentType 设置为 application/json?
  • 是的,但这不是原因。无论如何都是好点
  • 能否提供完整的代码;我收到 AppleIapResponse 错误!

标签: .net ios xamarin.ios in-app-purchase app-store-connect


【解决方案1】:

这是代码中的一个简单错误,我将收据写入 POST,而不是 JSON:

byte[] byteArray = Encoding.UTF8.GetBytes(json);
                                          ^^^^

我的头撞到墙上很痛..

【讨论】:

    猜你喜欢
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多