0

I am trying to accept BrainTree on my app. I don't understand the Async HTTP Client part.

Where do I place this in my app? Currently, I have it in the MainActivity and it is giving me errors for 'get', 'TextHttpResponseHandler' as well as 'clientToken'.

Any ideas?

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);
    }
}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Alex
  • 69
  • 5
  • What are the exact errors it is giving you? And I assume that you actually have something better than "your-server" in the URL...? :) – Andy Turner Jul 07 '15 at 23:17
  • Cannot resolve symbol for get and clientToken and then return statement required for TextHttpResponseHandler. And yes I am. – Alex Jul 07 '15 at 23:18
  • [`AsyncHttpClient`](https://github.com/AsyncHttpClient/async-http-client) is a third party library, you will need to add it as a dependency of your application before you can use it. – Luke Aug 04 '15 at 17:53

1 Answers1

0

Your call to client.get() is not in a method body, so your code is not syntactically valid. Try putting it in a method (or constructor or initializer block - but it's a bad idea to do work in either of those places).

For example, you could put that statement in a new method called 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) { ... }
}

You then need to invoke fetchClientToken somehow, just like you would with any other method.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243