ios – AFNetworking和Swift

前端之家收集整理的这篇文章主要介绍了ios – AFNetworking和Swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 swift获取 JSON响应.
我嗅到请求和响应 – >一切还好
但是返回值总是为零.
let httpClient = AppDelegate.appDelegate().httpRequestOperationManager as AFHTTPRequestOperationManager;

    let path = "/daten/wfs";
    let query = "?service=WFS&request=GetFeature&version=1.1.0&typeName=ogdwien:AMPELOGD&srsName=EPSG:4326&outputFormat=json".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding);

    func successBlock(operation: AFHTTPRequestOperation!,responSEObject: AnyObject!) {
        println("JSON: " + "\(responSEObject)")
    }

    func errorBlock(operation: AFHTTPRequestOperation!,error:NSError!) {
        println("Error: " + error.localizedDescription)
    }

    let urlString = "\(path)" + "/" + "\(query)"
    println("urlString: " + httpClient.baseURL.absoluteString + urlString)

我也试过这样:

httpClient.GET(urlString,parameters: nil,success: { (operation: AFHTTPRequestOperation!,responSEObject: AnyObject!) -> Void in
            println("Success")
            println("JSON: " + "\(responSEObject)")
        },failure:{ (operation: AFHTTPRequestOperation!,error:NSError!) -> Void in
            println("Failure")
        })

…但是responSEObject总是似乎为零

[编辑]
也许原因是我的AppDelegate中可能出错的初始化:

var httpRequestOperationManager: AFHTTPRequestOperationManager? // JAVA SERVER Client

class func appDelegate() -> AppDelegate {
    return UIApplication.sharedApplication().delegate as AppDelegate
}

func configureWebservice() {
    let requestSerializer = AFJSONRequestSerializer()
    requestSerializer.setValue("1234567890",forHTTPHeaderField: "clientId")
    requestSerializer.setValue("Test",forHTTPHeaderField: "appName")
    requestSerializer.setValue("1.0.0",forHTTPHeaderField: "appVersion")

    let responseSerializer = AFJSONResponseSerializer()

    AFNetworkActivityIndicatorManager.sharedManager().enabled = true

    // ##### HTTP #####
    let baseURL = NSURL(string: "http://data.wien.gv.at");
    httpRequestOperationManager = AFHTTPRequestOperationManager(baseURL: baseURL))

    httpRequestOperationManager!.requestSerializer = requestSerializer
    httpRequestOperationManager!.responseSerializer = responseSerializer
}

任何建议我做错了什么?

亲爱的,史蒂夫

解决方法

@H_404_24@ Swift与Objective-C代码完全兼容,所以您的问题与Swift本身无关.在AFNetworking中,responSEObject有时可以为零.其中包括

> A 204未返回任何内容状态代码,
>如果输出流设置为写入文件,
>如果验证期间的错误不是NSURLErrorCannotDecodeContentData(例如不可接受的内容类型)

查看#740#1280了解更多信息.

原文链接:https://www.f2er.com/iOS/335947.html

猜你在找的iOS相关文章