2013-05-17 62 views
0

我有一个在市场上的应用程序。我的下一个版本我想实施应用内购买。我一直在关注setup of v3 of in-app billing的(长)指南。我从该链接完成了前3个课程,但是现在我的代码有一大堆错误,这些错误都在同一个变量上。设置应用程序内计费v3

在下面的代码,spreadsheet是从我的开发者控制台正在购买什么我PRODUCT_ID。每次使用变量spreadsheet时,错误是:“电子表格无法解析为变量。”

public class BibleStudy extends SwarmActivity { 
     Context c; 
     IabHelper mHelper; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.biblestudy); 

      c = this; 

     String base64EncodedPublicKey = a + b + d + e + f + g + h + i + j + k; 

     // Create the helper, passing it our context and the public key to verify signatures with 
     mHelper = new IabHelper(this, base64EncodedPublicKey); 

     // Start setup. This is asynchronous and the specified listener will be called once setup completes. 
     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
      public void onIabSetupFinished(IabResult result) { 

       if (!result.isSuccess()) { 
        // Oh noes, there was a problem. 
        complain("Problem setting up in-app billing: " + result); 
        return; 
       } 

       // Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own. 
       mHelper.queryInventoryAsync(mGotInventoryListener); 
      } 
     }); 
    } 

    // Listener that's called when we finish querying the items and subscriptions we own. 
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 
     public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
      if (result.isFailure()) { 
       complain("Failed to query inventory: " + result); 
       return; 
      } 

      /* 
      * Check for items we own. Notice that for each purchase, we check 
      * the developer payload to see if it's correct! See 
      * verifyDeveloperPayload(). 
      */ 

      // Do we have the premium upgrade? 
      Purchase spreadsheetPurchase = inventory.getPurchase(spreadsheet); 

      updateUi(); 
     } 
    }; 

    // User clicked the "Download Spreadsheet!" button. 
    public void onBuyButtonClick(View v) { 

     // launch the gas purchase UI flow. 
     // We will be notified of completion via mPurchaseFinishedListener 

     /* TODO: for security, generate your payload here for verification. See the comments on 
     *  verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use 
     *  an empty string, but on a production app you should carefully generate this. */ 
     String payload = ""; 

     mHelper.launchPurchaseFlow(this, spreadsheet, 10001, mPurchaseFinishedListener, payload); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     // Pass on the activity result to the helper for handling 
     if(!mHelper.handleActivityResult(requestCode, resultCode, data)) { 
      // not handled, so handle it ourselves (here's where you'd 
      // perform any handling of activity results not related to in-app 
      // billing... 
      super.onActivityResult(requestCode, resultCode, data); 
     } 
    } 

    /** Verifies the developer payload of a purchase. */ 
    boolean verifyDeveloperPayload(Purchase p) { 
     String payload = p.getDeveloperPayload(); 

     /* 
     * TODO: verify that the developer payload of the purchase is correct. It will be 
     * the same one that you sent when initiating the purchase. 
     * 
     * WARNING: Locally generating a random string when starting a purchase and 
     * verifying it here might seem like a good approach, but this will fail in the 
     * case where the user purchases an item on one device and then uses your app on 
     * a different device, because on the other device you will not have access to the 
     * random string you originally generated. 
     * 
     * So a good developer payload has these characteristics: 
     * 
     * 1. If two different users purchase an item, the payload is different between them, 
     * so that one user's purchase can't be replayed to another user. 
     * 
     * 2. The payload must be such that you can verify it even when the app wasn't the 
     * one who initiated the purchase flow (so that items purchased by the user on 
     * one device work on other devices owned by the user). 
     * 
     * Using your own server to store and verify developer payloads across app 
     * installations is recommended. 
     */ 

     return true; 
    } 

    // Callback for when a purchase is finished 
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
     public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
      if(result.isFailure()) { 
       complain("Error purchasing: " + result); 
       return; 
      } 

      if(!verifyDeveloperPayload(purchase)) { 
       complain("Error purchasing. Authenticity verification failed."); 
       return; 
      } 

      else if(purchase.getSku().equals(spreadsheet)) { 
       // bought the premium upgrade! 
       alert("Thank you for upgrading to premium!"); 
//    mIsPremium = true; 
       updateUi(); 
      } 
     } 
    }; 

    // updates UI to reflect model 
    public void updateUi() { 

     // MATT: update a TextView to show link after purchase is done. 

//  // update the car color to reflect premium status or lack thereof 
//  ((ImageView)findViewById(R.id.free_or_premium)).setImageResource(mIsPremium ? R.drawable.premium : R.drawable.free); 
// 
//  // "Upgrade" button is only visible if the user is not premium 
//  findViewById(R.id.upgrade_button).setVisibility(mIsPremium ? View.GONE : View.VISIBLE); 
// 
//  // "Get infinite gas" button is only visible if the user is not subscribed yet 
//  findViewById(R.id.infinite_gas_button).setVisibility(mSubscribedToInfiniteGas ? 
//    View.GONE : View.VISIBLE); 
// 
//  // update gas gauge to reflect tank status 
//  if (mSubscribedToInfiniteGas) { 
//   ((ImageView)findViewById(R.id.gas_gauge)).setImageResource(R.drawable.gas_inf); 
//  } 
//  else { 
//   int index = mTank >= TANK_RES_IDS.length ? TANK_RES_IDS.length - 1 : mTank; 
//   ((ImageView)findViewById(R.id.gas_gauge)).setImageResource(TANK_RES_IDS[index]); 
//  }   
    } 

    void complain(String message) { 
     alert("Error: " + message); 
    } 

    void alert(String message) { 
     AlertDialog.Builder bld = new AlertDialog.Builder(this); 
     bld.setMessage(message); 
     bld.setNeutralButton("OK", null); 
     bld.create().show(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if(mHelper != null) mHelper.dispose(); 
     mHelper = null; 
    } 
} 

购买成功完成后,我想要做的是向用户提供链接。所以我想我需要的是我可以验证购买完成的东西,然后我可以显示先前不可见的TextView,为他们提供链接。

我的问题是我做了什么错误的教程,我怎么能得到的错误消失,每个人都出现在变量spreadsheet使用?

+0

也许我理解你错了,而是把它在,可以从罐子没有按提取一个“看不见”的字符串保护你希望人们购买内容对我来说看起来并不安全 – LionC

+0

你理解正确。我对此很感兴趣,并希望提供建议。什么是处理这个问题的最佳途径? – Matt

+0

因为我真的没有进入他们商店的Adroid开发和应用内购买结构,所以我无法为您提供直接的工作模式,但我猜Web可以。但是,如果您的应用程序没有某种类型的服务器 - 客户端系统,则不必担心这些东西太多,因为人们总是可以修改本地的东西,因此它可以做他想做的事情。 – LionC

回答

0

想通了。我不得不声明spreadsheet PRODUCT_ID。我在想这是从服务器拉出来的。

我综合解决问题的代码:

static final String SKU_SPREADSHEET = "spreadsheet";