在Android上的应用程序购买中实现

时间:2015-06-07 21:30:59

标签: android eclipse multithreading in-app-purchase

我正在为eclipse购买我的应用程序而实施,但我坚持这一部分,你必须调用getSkuDetails

Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);

Android实施指南 - > http://developer.android.com/google/play/billing/billing_integrate.html

告诉我

"警告:不要在主线程上调用getSkuDetails方法。调用此方法会触发可能阻止主线程的网络请求。相反,创建一个单独的线程并从该线程内部调用getSkuDetails方法。"

我唯一的问题是我不知道如何在我的主要活动中调用另一个线程或如何去做,因为我之前从未做过,感谢任何可以帮助我坚持这个的人过去一周

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    connection = new ServiceConnection() {

           @Override
           public void onServiceDisconnected(ComponentName name) {

               mService = null;

           }

           @Override
           public void onServiceConnected(ComponentName name, IBinder service) {

               mService = IInAppBillingService.Stub.asInterface(service);

           }
        };


        Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");

        serviceIntent.setPackage("com.android.vending");
        bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);


}

public void levelTwoButtonClicked(){

    Assets.click.play(1.00f);

    new LoadIconTask().execute();

}

class LoadIconTask extends AsyncTask<Integer, Integer, Long> {

    @Override
    protected Long doInBackground(Integer... params) {

        ArrayList<String> skuList = new ArrayList<String>();
        skuList.add(inappid);
        Bundle querySkus = new Bundle();
        querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
        Bundle skuDetails;      

    try{

            skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);

            int response = skuDetails.getInt("RESPONSE_CODE");
            if(response == 0){

                ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");

                for(String thisResponse : responseList){

                    JSONObject object = new JSONObject(thisResponse);
                    String sku = object.getString("productID");
                    String price = object.getString("price");

                    if(sku.equals(inappid)){

                        System.out.println("price" + price);
                        Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                        PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
                        startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));

                    }

                }

            }
        } catch (RemoteException e){
            e.printStackTrace();
        } catch (JSONException e){
            e.printStackTrace();
        } catch (SendIntentException e){
            e.printStackTrace();
        }


        return null;
    }


}

每次我跑步并点击我的买入等级两个按钮我都会收到此错误

06-07 20:16:26.102: E/AndroidRuntime(19783): FATAL EXCEPTION: AsyncTask #2
06-07 20:16:26.102: E/AndroidRuntime(19783): java.lang.RuntimeException: An error occured while executing doInBackground()
06-07 20:16:26.102: E/AndroidRuntime(19783):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at java.lang.Thread.run(Thread.java:856)
06-07 20:16:26.102: E/AndroidRuntime(19783): Caused by: java.lang.NullPointerException
06-07 20:16:26.102: E/AndroidRuntime(19783):    at com.izzymachado.framework.implementation.AndroidGame$LoadIconTask.doInBackground(AndroidGame.java:198)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at com.izzymachado.framework.implementation.AndroidGame$LoadIconTask.doInBackground(AndroidGame.java:1)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-07 20:16:26.102: E/AndroidRuntime(19783):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

第198行是skuDetails = mService.getSkuDetails(3,getPackageName(),&#34; inapp&#34;,querySkus);

1 个答案:

答案 0 :(得分:1)

您不能在主线程上进行网络呼叫,这是因为您不希望阻止用户在发生这种情况时执行操作。

因此,您应该使用AsyncTask进行此操作,这将在后台进行网络调用,然后通知您,以便您可以对UI进行更改(返回UI线程)。