Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift Managing Complexity Grand Central Dispatch

When I try to make a HTTP request , I keep getting in my console - " Error : Not a valid HTTP response " ??

I have also tried the project file from the teacher's notes but still to no avail .

Without you're code in the NetworkOperation class, it will be quite difficult to help.

class NetworkOperation {

lazy var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
lazy var session: NSURLSession = NSURLSession(configuration: self.config)
let queryURL: NSURL

typealias JSONDictionaryCompletion = ([String: AnyObject]? -> Void)

init(url: NSURL) {
    self.queryURL = url
}

func downloadJSONFromURL(completion: JSONDictionaryCompletion) {

    let request = NSURLRequest(URL: queryURL)
    let dataTask = session.dataTaskWithRequest(request) {
        (let data, let response, let error) in

        // 1. Check HTTP response for successful GET request
        if let httpResponse = response as? NSHTTPURLResponse {
            switch httpResponse.statusCode {
            case 200:
                // 2. Create JSON object with data
                let jsonDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? [String: AnyObject]
                completion(jsonDictionary)
            default:
                println("GET request not successful. HTTP status code: \(httpResponse.statusCode)")
            }
        } else {
            println("Error: Not a valid HTTP response")
        }
    }

    dataTask.resume()
}

}

2 Answers

It's not working for me either. I get error: NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

I got this with the Blog Reader app from the Objective-C course as well, but it was solved by adding this code:

<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>

-to disable App Transport Security. This solution is not working for this app however. I tried many different configurations of NSAppTransport security for hours, but I don't get anything but that error. I don't think it's the code because I get a response from other tested API calls. I think it has something to do with security changes in IOS 9 but changing the deployment target doesn't change anything either. I know the URL is right because I get a response in Google Chrome. Any help in this regard would be really appreciated.

EDIT: So I tried the exact same code at home and it worked. I think there are networking blocks keeping this from getting through at my college, but only from the simulator? As I said it works if I paste the API call into Chrome. Same could be true of you if you have this issue, be warned!

Christopher Augg
Christopher Augg
21,223 Points

Navneet,

You have a typo in your typealias:

typealias JSONDictionaryCompletion = ([String: AnyObject]? -> Void)

It should be:

typealias JSONDictionaryCompletion = ([String: AnyObject]?) -> Void

Notice the placement of the closing )

I hope this helps.

Regards,

Chris