How to get Android code equivalent to Swift

I have created an app and am using the Firebase backend for both Android and IOS. The problem, I am having is:

I have a firebase-function that collects the ‘uid’ and ‘email’ from payoutRequest() and then sends the information to Paypal so that the driver can be paid from PayPal.

this Android code works:

public static final MediaType MEDIA_TYPE = MediaType.parse(“application/json”); ProgressDialog progress;

private void payoutRequest() {

progress = new ProgressDialog(this);
progress.setTitle("Processing your payout ...");
progress.setMessage("Please Wait .....");

// HTTP Request ....
final OkHttpClient client = new OkHttpClient();

// in json - we need variables for the hardcoded uid and Email
JSONObject postData = new JSONObject();

try {
    postData.put("uid", FirebaseAuth.getInstance().getCurrentUser().getUid());
    postData.put("email", mPayoutEmail.getText().toString());

} catch (JSONException e) {

// Request body ...
RequestBody body = RequestBody.create(MEDIA_TYPE, postData.toString());

// Build Request ...
final Request request = new Request.Builder()
        .addHeader("Content-Type", "application/json")
        .addHeader("cache-control", "no-cache")
        .addHeader("Authorization", "Your Token")

client.newCall(request).enqueue(new Callback() {
    public void onFailure(Call call, IOException e) {
        // something went wrong right off the bat

    public void onResponse(Call call, Response response) throws IOException {
        // response successful ....
        // refers to response.status('200') or ('500')
        int responseCode = response.code();
        if (response.isSuccessful()) {
            switch(responseCode) {
                case 200:
                            "Payout Successful!", Snackbar.LENGTH_LONG)

                case 500:
                            "Error: no payout available", Snackbar

                            "Error: couldn't complete the transaction",

        } else {
                    "Error: couldn't complete the transaction",



this SWIFT code isn’t working like it should be like the Android code:

In an attempt to create an equivalent of the Android code, I did this:

let params: Parameters = [“uid”: FIRAuth.auth()?.currentUser!.uid as Any, “email”: txtPayoutEmail.text!]

    let url = ""

    let headers: HTTPHeaders = [
        "Content-Type": "application/json",
        "cache-control": "no-cache",
        "Authorization": "Your Token"
    let postData = try! params, options: [])
    print("postData: \(postData)")
    let request = NSMutableURLRequest(url: NSURL(string: url)! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
    request.httpMethod = "POST"
    request.httpBody = postData as Data
    request.allHTTPHeaderFields = headers
    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("status code: \(httpStatus.statusCode)")
            print("response: \(String(describing: response))")
        if (error != nil) {
            print("No data found: \(error!)")
        } else {
            _ = response as? HTTPURLResponse
            //                print(httpResponse!)
            let parsedObject = try! JSONSerialization.jsonObject(with: postData, options: .allowFragments)


What it does and doesn’t do:

When I run the above SWIFT code, it collects the ‘uid’ and ‘email’ in the firebase-functions logs but it doesn’t send it to Paypal using the same index.js file I am using with Android.

I know it is not the index file because it works for android, so it has to do with the request I am sending in SWIFT.

For a reference of the post I put up on stackoverflow, please see this post:

how to get Swift equivalent from Android code

Please help!

@macsimus Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.