Swift访问REST Web服务失败

时间:2014-11-08 03:33:36

标签: ios web-services rest

按照本教程tutorial

我可以调用基于springMVC发布的我的网络服务,但是 connectionDidFinishLoading 似乎没有触发,也没有任何错误消息。

这是我的代码。

import Foundation

class Requestor {

    var data: NSMutableData = NSMutableData();

    func connect() {
        var urlParth =  "http://localhost:8080/webservice/request/public/key";

        var url = NSURL(string: urlParth);
        var request:NSURLRequest = NSURLRequest(URL: url!);
        var connection = NSURLConnection(request: request, delegate: self, startImmediately: false);

        connection?.start();
    }

    //NSURLConnection Connection failed
    func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
        println("Failed with error:\(error.localizedDescription)")
    }

    func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        println("didReceiveResponse")
    }

    func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
        self.data.appendData(conData)
    }

    func connectionDidFinishLoading(connection: NSURLConnection!) {
        println(self.data)
        println("did");
    }
}

这是我的服务代码:

@RestController
@RequestMapping("/webservice")
public class PublicService extends Service {

    @RequestMapping("/request/public/key")
    public String requestPublicKey() {
        String result = String.format("{\"code\":\"%s\",\"publicKey\":\"%s\"}", "200", Base64.encodeBase64String(rasFactory.getPublicKeyInByte()));
        return result;
    }
}

我通过在服务中设置断点调试了很长时间,似乎我可以达到服务,但是在服务返回结果后,iOS方面没有任何反应。

我的代码有什么问题吗?

感谢。

0 个答案:

没有答案