Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking and Swift

I'm trying to get a JSON response using Swift.

I sniffed the request and response -> everything ok. However the return value is always nil.

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)

I also tried it this way:

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")
    })

... But the responseObject always seems to be nil

EDIT:

Maybe the reason is the possible wrong initialisation in my 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
}

Any suggestions what I'm doing wrong?

like image 491
user707342 Avatar asked Jun 05 '14 08:06

user707342


People also ask

What is AFNetworking used for?

What is AFNetworking? AFNetworking is an open source networking library for iOS and macOS that simplifies a developer's tasks with a RESTful networking API and creates modular request/response patterns with success, progress, and failure completion blocks.

What is AFNetworking framework?

AFNetworking is a delightful networking library for iOS, macOS, watchOS, and tvOS. It's built on top of the Foundation URL Loading System, extending the powerful high-level networking abstractions built into Cocoa. It has a modular architecture with well-designed, feature-rich APIs that are a joy to use.


1 Answers

Swift is fully compatible with Objective-C code, so your problem is not connected with Swift itself. In AFNetworking, the responseObject can sometimes be nil. This includes cases, where:

  • A 204 No Content status code was returned,
  • If output stream was set to write to file,
  • If the error during validation wasn't NSURLErrorCannotDecodeContentData (e.g. unacceptable content type)

Check out #740 and #1280 for more information.

like image 133
akashivskyy Avatar answered Sep 17 '22 08:09

akashivskyy