简体   繁体   English

未执行iOS中Swift的Rest API

[英]Rest API by Swift in IOS is not executed

I am using REST API to get data from server using swift 2. When I call the function below, something happens, and the func getAllServiceName(data :NSData) is not executed. 我正在使用REST API使用swift 2从服务器获取数据。当我调用下面的函数时,发生了某些事情,并且func getAllServiceName(data :NSData)未执行。 I tried many things but I did not have success. 我尝试了很多事情,但没有成功。

func getServiceName () {
    let urlServiceName = NSURL(string: "urllinkOfMe")
    let request = NSURLRequest(URL: urlServiceName!)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
        self.getAllServiceName(data!)
    }
}

func getAllServiceName(data :NSData)
{
    let dictServicename: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary

    if(dictServicename["data"] != nil){
        let dataTable :NSArray = dictServicename.valueForKey("data") as! NSArray
        for var i = 0 ; i < dataTable.count ; i++
        {
            //pickOptionVas.addObject(dataTable[i].valueForKey("serviceName")!)
        }
    }
}

Here is what you used to get data from server by calling API: 这是您过去通过调用API从服务器获取数据的方式:

func getServiceName () {

    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)

    let urlServiceName = NSURL(string: "http://188.166.232.101:1000/hs/vasService/getServiceName")
    let request = NSMutableURLRequest(URL: urlServiceName!)
    request.HTTPMethod = "GET"
    let postTask = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

        print(data)
    })
    //This is necessary
    postTask.resume()
}

Go for the above code and let me know you are getting the data or not! 输入上面的代码,让我知道您是否正在获取数据!

在此处输入图片说明

What's the response code? 响应码是什么? if you get 500 perhaps something wrong with your request parameter or your REST API is not implemented correctly. 如果您得到500,则可能是您的请求参数有问题,或者您的REST API未正确实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM