1

I am trying to download some XML from the Stack Exchange API. However, I'm not getting any results back. The URL brings up the XML when accessed in the browser, but I can't seem to get that in the form of an NSData object. Or rather, I keep ending up with NSZeroData. Here is my code, note that it is written in Swift:

import UIKit
import XCPlayground

XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

var myRequest = NSURLRequest(URL:NSURL(string: "api.stackexchange.com/answers?site=stackoverflow"))

var mySession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())

var dataTask = mySession.dataTaskWithRequest(myRequest, completionHandler: { (data, response, error) in
    var myData = data //myData now equals NSZeroData
})
dataTask.resume()
PopKernel
  • 4,110
  • 5
  • 29
  • 51

2 Answers2

0

Try:

var myRequest = NSURLRequest(URL:NSURL(string: "http://api.stackexchange.com/answers?site=stackoverflow"))

Diff is the protocol "http"

Danoli3
  • 3,203
  • 3
  • 24
  • 35
0

One solution that worked for me,

if your customersString in NSURL *url = [NSURL URLWithString: customersString]; targets a php-file on your server (maybe a corner-case):

Check, whether you use the right PHP-version on your server! This can - for instance - be done with a new php-file:

<?php phpinfo(); ?>

Explanation:

Your php-file may build your desired JSON (array) response not in a correct way, since php-version 5.4 builds arrays in a different way (compare: Link to PHP.net):

<?php
    //before PHP 5.4
    $array = array(
        "foo" => "bar",
        "bar" => "foo",
    );
    // since PHP 5.4
    $array = [
        "foo" => "bar",
        "bar" => "foo",
    ];
?>

This may lead to a NSZeroData response, due to that corrupted JSON.

Thus: Changing the PHP-version on my server solved the issue for me. Hope this helps!!!

esvau
  • 744
  • 5
  • 8