LizG
LizG

Reputation: 2530

How to translate my Android code to Swift code for my Firebase function?

From my research, there isn't much help translating Android code to Swift code. With some help, we were able to translate or convert some of the code but it's not quite finished. When I run the code, I get an error:

Response could not be serialized, input data was nil or zero length.

responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

Android code needing converting to Swift code:

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 .....");
progress.setCancelable(false);
progress.show();

// 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) {
    e.printStackTrace();
}

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

// Build Request ...
final Request request = new Request.Builder()
        .url("https://us-central1-myapp.cloudfunctions.net/payout")
        .post(body)
        .addHeader("Content-Type", "application/json")
        .addHeader("cache-control", "no-cache")
        .addHeader("Authorization", "Your Token")
        .build();

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

    @Override
    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:
                    Snackbar.make(findViewById(R.id.layout),
                            "Payout Successful!", Snackbar.LENGTH_LONG)
                            .show();
                    break;

                case 500:
                    Snackbar.make(findViewById(R.id.layout),
                            "Error: no payout available", Snackbar
                                    .LENGTH_LONG).show();
                    break;

                default:
                    Snackbar.make(findViewById(R.id.layout),
                            "Error: couldn't complete the transaction",
                            Snackbar.LENGTH_LONG).show();
                    break;
            }

        } else {
            Snackbar.make(findViewById(R.id.layout),
                    "Error: couldn't complete the transaction",
                    Snackbar.LENGTH_LONG).show();
        }

        progress.dismiss();
    }
});
}

Swift code used from the above Android code:

let url = "https://us-central1-myapp.cloudfunctions.net/payout"
let headers: HTTPHeaders = [
    "Content-Type": "application/json",
    "cache-control": "Your Token"]

Alamofire.request(url, method: .get, headers: headers).validate().responseJSON { (response) in
     switch response.result {
        case .success(let value):
            // you fall here once you get 200 success code, because you use .validate() when you make call.
            print(value)
            // parse your JSON here.
            let parameters : [String: Any] =
                ["uid": FIRAuth.auth()!.currentUser!.uid,
                 "email": self.paypalEmailText.text!]

            let postData = try! JSONSerialization.data(withJSONObject: parameters, options: [])

        case .failure(let error):
            if response.response?.statusCode == 500 {
                print("Error no payout available")
                print(error.localizedDescription)
            } else {
                print("Error: couldn't complete the transaction")
                print(error.localizedDescription)
            }
        }
}

How can I convert the Android code into the Swift code or discover what it is I am doing wrong? This code is used to post to the function I have created for Firebase.

Edit

With the help of supplied code in this post, I was able to come up with this code but it is still coming up with the same error:

===========Error===========
Error Code: 4
Error Messsage: Response could not be serialized, input data was nil or zero length.
response FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

updated swift code

let url = "https://us-central1-myapp.cloudfunctions.net/payout"
    let headers: HTTPHeaders = [
        "Content-Type": "application/json",
        "cache-control": "Your Token"]

    let params : [String: Any] = [
        "uid": FIRAuth.auth()!.currentUser!.uid,
        "email": self.paypalEmailText.text!]

    Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).validate().responseJSON { (response) in
         switch response.result {
            case .success(let JSON):
               print("Success with JSON: \(JSON)")
               if (response.result.value as? [String:AnyObject]) != nil {
                // Access your response here
                print(response.result.value!)
               }

            break

            case .failure(let error):
                if response.response?.statusCode == 500 {
                    print("Error no payout available")
                    print(print("Request failed with error: \(error.localizedDescription)"))

                } else {
                    print("Error: couldn't complete the transaction")
                    print("\n\n===========Error===========")
                    print("Error Code: \(error._code)")
                    print("Error Messsage: \(error.localizedDescription)")

                }
            }

         print("response \(response)")
    }

EDIT #2

I edited my method:

let url = "https://us-central1-myapp.cloudfunctions.net/payout"

    let headers: HTTPHeaders = [
        "cache-control": "no-cache",
        "Authorization": "Your Token",
        "Content-Type": "application/json"]

    let parameters : [String: Any] = [
            "uid": uid,
            "email": self.paypalEmailText.text!
    ]

    Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).validate(statusCode: 200..<600).responseJSON { (response) in

        print("Request: \(String(describing: response.request))") // original url request
        print("Result: \(response.result)") // response serialization result

        if response.response?.statusCode == 200 {

            print("Success with JSON: \(String(describing: response.result.value))")

        } else {
            let error = (response.result.value  as? [[String : AnyObject]])
            print(error as Any)
        }

        print("response \(response)")
    }

The response and print outs are:

Request: Optional(https://us-central1-myapp.cloudfunctions.net/payout)
Result: FAILURE
Success with JSON: nil
response FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

Keep in mind, in my url, my app is not called "myapp" it is just there for protection.

Upvotes: 0

Views: 143

Answers (2)

makle
makle

Reputation: 1267

So I'd do it like this here:

    let url = "https://us-central1-myapp.cloudfunctions.net/payout"
    let headers: HTTPHeaders = [
        "Content-Type": "application/json",
        "cache-control": "Your Token"
    ]

    Alamofire.request(url,
                      method: .get,
                      encoding: JSONEncoding.default,
                      headers: headers).responseJSON
        { response in
            switch response.result {
            case .success(let JSON):
                print("Success with JSON: \(JSON)")
                // parse your JSON here something like
                if let json = response.result.value as? [String:AnyObject] {
                    // Access your response here
                }
                break
            case .failure(let error):
                print("Request failed with error: \(error)")
            }
    }

In the success part you should be able to access the JSON and are able to parse it. Can you comment how your reponse object looks like, then I'll comment how you parse it and access the correct elements. I can otherwise only guess.

Upvotes: 1

Ann Catherine Jose
Ann Catherine Jose

Reputation: 216

I think there are two issues in the code:

  1. In the Android code, you are setting the parameters uid and email in the request body, whereas in the Swift code, you are setting these parameters in the response body, which is wrong (because by the time you got the response, the request is already completed without the params).
  2. If you want to set a body for the request, the HTTP method (the second parameter to Alamofire.request should be post instead of get.

What you need to do is set the parameters in the request body and set the HTTP method as post, as follows:

let parameters: [String: Any] =
             ["uid": FIRAuth.auth()!.currentUser!.uid,
              "email": self.paypalEmailText.text!]

Alamofire.request(url, method:.post, parameters:parameters, 
                  encoding: JSONEncoding.default, headers:headers)

Try it out and see if it works.

Upvotes: 2

Related Questions