配置Braintree

时间:2015-07-07 23:14:32

标签: java android braintree asynchttpclient

我想在我的应用上接受BrainTree。我不了解Async HTTP Client部分。

我在哪里放置这个应用程序?目前,我已在MainActivity中找到了它,并且它给了我{' get',' TextHttpResponseHandler'以及' clientToken'。

有什么想法吗?

package com.example.android.payments;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.loopj.android.http.*;
import com.braintreepayments.api.dropin.BraintreePaymentActivity;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    AsyncHttpClient client = new AsyncHttpClient();
    client.get("https://your-server/client_token", new TextHttpResponseHandler() {
        @Override
        public void onSuccess(String clientToken) {
            this.clientToken = clientToken;
        }
    });
    public void onBraintreeSubmit(View v) {
        Intent intent = new Intent(context, BraintreePaymentActivity.class);
        intent.putExtra(BraintreePaymentActivity.EXTRA_CLIENT_TOKEN, clientToken);
        // REQUEST_CODE is arbitrary and is only used within this activity.
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

您对client.get()的调用不在方法体中,因此您的代码在语法上无效。尝试将它放在一个方法(或构造函数或初始化程序块 - 但在这两个地方工作是个坏主意。)

例如,您可以将该语句放在名为fetchClientToken()的新方法中:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) { ... }

    AsyncHttpClient client = new AsyncHttpClient();

    public void fetchClientToken() {
      client.get("https://your-server/client_token", new TextHttpResponseHandler() {
          @Override
          public void onSuccess(String clientToken) {
              this.clientToken = clientToken;
          }
      });
    }

    public void onBraintreeSubmit(View v) { ... }
    @Override public boolean onCreateOptionsMenu(Menu menu) { ... }
    @Override public boolean onOptionsItemSelected(MenuItem item) { ... }
}

然后你需要以某种方式调用fetchClientToken,就像你使用任何其他方法一样。