2

I have a node.js server running on localhost, and using an Android app I am trying to generate a client token with the code on the server; however, when using a AsyncHttpClient .get() method which creates a new TextHttpResponseHandler, neither the Success or Failed methods are called. I can open localhost:3000 in Chrome and I get the client token no worries, but no response on the Android app.

Node.js Server:

var braintree = require('braintree')
var express = require('express')
var app = express()

var gateway = braintree.connect({
  accessToken: "access_token removed"
});

app.listen(3000);


app.get("/client_token", function (req, res) {
  gateway.clientToken.generate({}, function (err, response) {
    res.send(response.clientToken);
  });
});

Android Code:

private void generateClientToken() {

    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://localhost:3000/client_token", new TextHttpResponseHandler() {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            Log.i(TAG, "onFailure: ");
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            Log.i(TAG, "onSuccess: ");
            CLIENT_TOKEN = responseString;
        }
    });
}

I have tried using the runOnUIThread() method, but I get the same result. To reiterate, neither functions are run, the logs are not executed and the client token variable is not changed when this method is run.

Any help appreciated! Cheers!

Liam G
  • 771
  • 2
  • 9
  • 26
  • Hello, are you using an emulator ? In that case, localhost is not the right url but someething like 10.0.2.2:3000 or 10.0.2.3:3000 – Yourange Jan 03 '19 at 13:17
  • Local host url will not be worked in real device. If you want to test it with then you must run your app in emulator. – Piyush Jan 03 '19 at 13:28

0 Answers0