3

I am getting stuck with HTTP request.it did not show any error.compiler reads the first two lines and skip the code to "task.resume()".i am fetching data with same code on other view controller but it creats problem here

func getCustomers()
{
    let url = NSURL(string: "myURL.com")
    let task = URLSession.shared.dataTask(with: url! as URL) {
        (data, response, error) in

        guard let _:Data = data, let _:URLResponse = response  , error == nil else {
            print("error: \(String(describing: error))")

            return
        }
        do
        {
            self.getcustomersArray = [GetCustomers]()
            //JSON Parsing
            if let data = data,

                let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
            {
                let results = json["Result"] as? [[String : Any]]

                let getCustomersObject:GetCustomers = GetCustomers()
                for result in results!
                {
                    getCustomersObject.ActivityPrefix = (result["ActivityPrefix"] as? String)!
                    getCustomersObject.CustomerID = (result["CustomerID"] as? String)!
                    getCustomersObject.CustomerName = (result["CustomerName"] as? String)!
                    getCustomersObject.TFMCustomerID = (result["TFMCustomerID"] as? String)!
                    getCustomersObject.ShortName = (result["ShortName"] as? String)!
                    getCustomersObject.UserRights = (result["UserRights"] as? Int)!

                    self.totalCustomers += self.totalCustomers
                }
                self.customerName = getCustomersObject.CustomerName

            }
        }//end Do
        catch
        {

        }
    }

    task.resume()
}
5
  • Its obvious because completion block of dataTask will call later when you get response Commented Apr 14, 2017 at 11:09
  • 2
    What is the question? Commented Apr 14, 2017 at 11:09
  • what is the problem it is creating ? Commented Apr 14, 2017 at 11:12
  • @ch umer solved your problem? Commented Apr 14, 2017 at 11:14
  • 1
    @NiravD thanks for your answer actually i am new in swift Commented Apr 14, 2017 at 11:14

1 Answer 1

1

Using Block GET/POST/PUT/DELETE:

 let request = NSMutableURLRequest(url: URL(string: "Your API URL here" ,param: param))!,
        cachePolicy: .useProtocolCachePolicy,
        timeoutInterval:"Your request timeout time in Seconds")
    request.httpMethod = "GET"
    request.allHTTPHeaderFields = headers as? [String : String] 

    let session = URLSession.shared

    let dataTask = session.dataTask(with: request as URLRequest) {data,response,error in
        let httpResponse = response as? HTTPURLResponse

        if (error != nil) {
         print(error)
         } else {
         print(httpResponse)
         }

        DispatchQueue.main.async {
           //Update your UI here
        }

    }
    dataTask.resume()

I think you dont mention line request.httpMethod = "GET"

Sign up to request clarification or add additional context in comments.

3 Comments

For a GET request an explicit URLRequest is not needed and in Swift 3 use native URLRequest rather than NSMutableURLRequest.
Thank you for share your knowledge. I mean that he missed request httpMethod.
Basically the code in the question is working, GET is the default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.